dimanche 13 avril 2014

c ++ - fonction publique de la classe de Base et Virtual - Stack Overflow


Can I call the base class method from the virtual methods of the derived class ??


Class A {

public:
virtual ~A();
virtual void some_function() {};
int Foo();
}

Class B : public A {

public:
B();
virtual void Bar() const = 0;
}

// B.cpp

void B::Bar() const {
x = Foo();
}

Can i call like above ?? Getting the following error:


error: no matching member function for call to 'Foo' not viable: no known conversion from B to A for object argument.




First you said B::Bar was abstract and then you implemented it. That makes no sense. You need to remove that = 0 on the end.




If i remove = 0 also its not working. If i remove const completely, then it is working.


virtual void Bar(); // B.h

void Bar() { } // B.cpp

But now i have another problem here,


There is another class C which tries to override the method in Bar in B, which inherits B.


Class C : public B {

virtual void Bar();

}

Now, if i use B.h for Class C, then it is throwing the error,


Error : Class C hides the hidden overloaded function.

Hidden overloaded virtual function declared here: different qualifiers (none vs const)



Can I call the base class method from the virtual methods of the derived class ??


Class A {

public:
virtual ~A();
virtual void some_function() {};
int Foo();
}

Class B : public A {

public:
B();
virtual void Bar() const = 0;
}

// B.cpp

void B::Bar() const {
x = Foo();
}

Can i call like above ?? Getting the following error:


error: no matching member function for call to 'Foo' not viable: no known conversion from B to A for object argument.



First you said B::Bar was abstract and then you implemented it. That makes no sense. You need to remove that = 0 on the end.



If i remove = 0 also its not working. If i remove const completely, then it is working.


virtual void Bar(); // B.h

void Bar() { } // B.cpp

But now i have another problem here,


There is another class C which tries to override the method in Bar in B, which inherits B.


Class C : public B {

virtual void Bar();

}

Now, if i use B.h for Class C, then it is throwing the error,


Error : Class C hides the hidden overloaded function.

Hidden overloaded virtual function declared here: different qualifiers (none vs const)


0 commentaires:

Enregistrer un commentaire