================================================== =====
UPDATE:
I think my original post was a little misleading. Here is what I indeed want to know:
When I use this->foo() in the following code, it works, even for a pure virtual function;
Code:
class A{
public:
A(){
this->foo();
}
~A(){}
virtual void foo()=0;
};
void A::foo(){ cout<<"Class A"<<endl; }
class B: public A{
public:
B(){
this->foo();
}
~B(){}
void foo(){
cout<<"Class B"<<endl;
}
};
int main()
{
B obj;
return 0;
}
But when I use foo() instead, I got the compile-time error
Code:
class A{
public:
A(){
foo();
}
~A(){}
virtual void foo()=0;
};
void A::foo(){ cout<<"Class A"<<endl; }
class B: public A{
public:
B(){
foo();
}
~B(){}
void foo(){
cout<<"Class B"<<endl;
}
};
int main()
{
B obj;
return 0;
}
So I wonder what's the difference between this->foo() and foo() in my code?
================================================== ========
Original Post:
Why constructor can not call a pure virtual function, even if it's defined?
Another question is what's the difference between this->foo() and foo() ?
In my opinion they are the same.
Code:
class A{
public:
A(){
//this->foo();
foo();
}
~A(){}
virtual void foo()=0;
};
void A::foo(){ cout<<"Class A"<<endl; }
class B: public A{
public:
B(){
//this->foo();
foo();
}
~B(){}
virtual void foo(){
cout<<"Class B"<<endl;
}
};
int main()
{
B obj;
return 0;
}