Prev: basic_string CoW concurrency: typo in N2668, or very subtle point?
Next: copy constructor for class with member pointer
From: Daniel Krügler on 10 Jul 2010 04:21 On 10 Jul., 16:06, Florian <flo...(a)wanadoo.fr> wrote: > > Defining the constructor in the class declaration causes (requests) it to be > > "inlined", such that code for the function doesn't normally get inserted into > > your compiler's object code except at the point where it's called--but it's > > not called from anywhere in your example and so no code is generated; the > > linker never sees it. (Of course, if the compiler opts to not inline the > > ctor afterall, then you would see the linker error in both of your examples.) > > I think it's wrong. > > struct A > { > A(); > virtual void foo() {} > > }; > > A::A() {foo();} > > These don't lead to linker error. This is different from the OP example, because A::foo is no pure virtual function above. > @Daniel Kr�gler: Are you sure it's a UB ? Il could make unexpected > behavior in inherited class because in constructor of base class, type > is base never derived, so it isn't call the expected function, but it > isn't undefined. It is undefined in the OP example, because the function he's referring to is pure virtual. The C++03 standard says in 10.4/6: "Member functions can be called from a constructor (or destructor) of an abstract class; the effect of making a virtual call (10.3) to a pure virtual function directly or indirectly for the object being created (or destroyed) from such a constructor (or destructor) is undefined." This makes sense, because what could be the valid outcome of such a call? HTH & Greetings from Bremen, - Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: John Shaw on 11 Jul 2010 04:52
On Jul 6, 9:18 pm, liam_herron <liam_her...(a)hotmail.com> wrote: > Why does this compile when I define the body of the constructor in the > header versus > when I define the body of the constructor in the cpp file? > > // Works > > class A > { > public: > A() {f();} > virtual ~A() {} > virtual void f() = 0; > int a1_; > > }; > > // Generates Linker Error > class A2 > { > public: > A2(); > virtual ~A2() {} > virtual void f() = 0; > int a2_; > > }; Whether it generates error is compiler implementation dependent. Logically it should always generate an error. The virtual table is generated during construction and released during destruction. The standard states that a virtual call to a pure virtual function in the constructor or destructor is undefined. class A { �virtual void f()=0; }; class B : public A { �void f(){...} }; The class 'A' constructor is called before the class 'B' constructor, so the pointer to the pure virtual function defined in class 'B' is not in the table yet. The call to the virtual functions is undefined, because at that point anything can happen. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |