undefined reference to vtable; virtual function issue [duplicate] - c++

This question already has answers here:
Undefined reference to vtable
(21 answers)
Closed 3 years ago.
Suppose I override a virtual function in a child class with a declaration, and do not give a definition for the method. For example:
class Base
{
virtual void f() = 0;
}
class Derived : public Base
{
void f();
}
(where I haven't given the definition of f). If I now use the class Derived - is it possible that i get a compiler error like "undefined reference to vtable..."?

Not for f. You've explicitly said that the function will not have an implementation, which implies that derived classes must implement it.
However, if you declare a pure virtual destructor (e.g., virtual ~Base()= 0), you will have to provide a definition somewhere. In that case, if you don't, you will get a "undefined reference to vtable..." error.

Related

Call pure virtual function from child constructor/destructor [duplicate]

This question already has answers here:
Calling virtual functions inside constructors
(15 answers)
Closed 1 year ago.
class A{
virtual void setEnable(bool enable) = 0;
};
class B : A{
B() {
setEnable(true);
}
~B() {
setEnable(false);
}
bool enable_ = false;
void setEnable(bool enable) override {
enable_ = enable;
}
};
Am I correct in understanding that the B :: setEnable function will be added to the vtable only after the constructor exits and this is undefined behavior?
Am I correct in understanding that the B::setEnable function will be added to the vtable only after the constructor exits and this is undefined behavior?
No. Inside the body of the A constructor, the A object is fully initialized, but the B object is not. The problem with calling a virtual function from a constructor is that it will unintuitively call the method of the same type that the constructor belongs to, without the polymorphic behavior.

C++ virtual destructor definitions [duplicate]

This question already has answers here:
g++ undefined reference to typeinfo
(18 answers)
Closed 6 years ago.
I have three classes
class A {
// pure virtual funcs and member vars
virtual ~A();
}
class B : public A {
// some more pure virtual funcs
virtual ~B();
}
class C : public B {
// concrete implementations
~C() {}
}
Presently this doesn't compile with an 'undefined reference to `typeinfo' error (~B() is not defined, easily fixable) however I'm wondering if just defining 'virtual ~B {}' is the correct thing to do or whether ~C should be virtual and defined so calls to ~B are dispatched to ~C?
Assuming that this question is about a pure virtual destructor (the code is not real, so it's difficult to say, but you're talking about a missing destructor definition, and about pure virtuals):
A pure virtual destructor that can be invoked, must be defined.
You can not define it in the class definition.
There is no clear reason why it must be defined external to the class definition, except an old comment by Bjarne Stroustrup (the language creator) that he viewed = 0 as indicating “no body”.
Example.
struct S
{
virtual ~S() = 0;
};
S::~S() {}

Private implementation for public interface method [duplicate]

This question already has answers here:
Overriding public virtual functions with private functions in C++
(7 answers)
Closed 7 years ago.
I have encountered a piece of code with method being exposed via public interface while the implementation is private. I'm not sure what should be the expected behavior. Simplified example:
#include <iostream>
class Interface
{
public:
virtual ~Interface() {}
virtual void myIfMethod() = 0;
};
class Derived : public Interface
{
private:
void myIfMethod(){std::cout << "private method invoked via public interface" << std::endl;}
};
int main()
{
Interface* myObj = new Derived;
myObj->myIfMethod();
delete myObj;
return 0;
}
This sample compiles and executes without a warning: http://ideone.com/1Ouwk4
Is this a correct and well-defined behavior? And if so, why?
Note, the question isn't about private interface method with public implementation (there are multiple such questions on SO) but the other way around.
C++ standard draft
Access to virtual functions [class.access.virt]
1 The access rules (Clause 11) for a virtual function are determined by its declaration and are not affected by
the rules for a function that later overrides it.
2 Access is checked at the call point using the type of the expression used to denote the object for which the member function is called (B* in the example above). The access of the member function in the class in which it was defined (D in the example above) is in general not known.
The function is called through a pointer of type Interface in which the member function is public, so the access is allowed. The access of the derived member function is not known and has no effect. The behaviour is correct and well defined as far as the standard is concerned.
Of course, it might be quite pointless to define the overriding function private since it's accessible through virtual dispatch anyway.

overriding virtual function to non-virtual function is OK? [duplicate]

This question already has answers here:
C++ "virtual" keyword for functions in derived classes. Is it necessary?
(9 answers)
Closed 9 years ago.
class Base
{
virtual void foo();
}
class Derived : public Base
{
void foo();
}
It it OK?
Or it can make some problems?
I think it insist "DO NOT INHERIT FROM DERIVED".
Once you marked the foo() function in Base class as virtual it is implicitly virtual in Derived class, even if you don't mention the keyword virtual in front of it.
virtualness is inherited. Even if you do not declare an overridden method virtual, it will be virtual.
Hence, if you access an object of Derived using Base pointer or reference, it will call foo of Derived.
virtual functions are inherited automatically. So even if you don't declare it as virtual, it is virtual actually.
The reason for you to explicitly declare it as virtual is for better clarification so that anyone reading this code can instantly understand that it is a virtual functions, without having to check the base class declarations.

Calling pure virtual function in constructor gives an error [duplicate]

This question already has answers here:
call to pure virtual function from base class constructor
(8 answers)
Closed 5 years ago.
class a //my base class
{
public:
a()
{
foo();
}
virtual void foo() = 0;
};
class b : public a
{
public:
void foo()
{
}
};
int main()
{
b obj; //ERROR: undefined reference to a::foo()
}
Why it gives me error? The pure virtual foo is defined. What do I need to change in my code to make it work? I need pure virtual method from base class be called in its constructor.
Calling virtual functions in a constructor is recognised as a bad thing to do.
During base class construction of a derived class object, the type of
the object is that of the base class. Not only do virtual functions
resolve to the base class, but the parts of the language using runtime
type information (e.g., dynamic_cast (see Item 27) and typeid) treat
the object as a base class type.
So your instantiation of b invokes the a constructor. That calls foo(), but it's the foo() on a that gets called. And that (of course) is undefined.
Quoted from a book "Let Us C++"by Yashwant Kanetkar
It is always the member function of the current class , is called.That
is , the virtual mechanism doesn't work within the constructor
So, the foo() of class a gets called.
Since it is declared pure virtual, it will report an error
foo function is called in class a's constructor, and at that time, object b has not been fully constructed yet, hence it's foo implementation is unavailable.
Quoted from "Effective C++":
Don't call virtual functions during construction or destruction,
because such calls will never go to a more derived class than that of
the currently executing constructor or destructor