Virtual Functions?
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
d
is initialized with the vtable ofBase
, that's the reason whyBase::foo1
is called. On the other hand it is declared as a pointer to aDerived
object, hence the compiler (you fooled) calls thefoo3
method. Tryclass Derived : public Base
{
int data;
public:
Derived() { data = 100;}
void foo1() { }
void foo3() { cout << data << endl;}
} ;Too see why fooling the compiler is not a good idea.
Veni, vidi, vici.
-
d
is initialized with the vtable ofBase
, that's the reason whyBase::foo1
is called. On the other hand it is declared as a pointer to aDerived
object, hence the compiler (you fooled) calls thefoo3
method. Tryclass Derived : public Base
{
int data;
public:
Derived() { data = 100;}
void foo1() { }
void foo3() { cout << data << endl;}
} ;Too see why fooling the compiler is not a good idea.
Veni, vidi, vici.
Hi, I have tried this. I am getting some junk value. Why is this happened? :( Regards msr
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
msr_codeproject wrote:
- How it is possible to call foo3() of derived class.
Just like you have it. What's the problem? :confused:
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Hi, I have tried this. I am getting some junk value. Why is this happened? :( Regards msr
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
OK, this is real simple. You instantiate an instance of the "Base" class and then you fool the compiler into thinking it's a "Derived" class object. Then you called a function that only exists in a "real" instantiation of a "Derived" class object. What you did was fool yourself into expecting something reasonable to happen and you're surprised when it doesn't.
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
-
class Base
{
public:
Base() { }
virtual void foo1() { }
virtual void foo2() { }} ;
class Derived : public Base
{
public:
Derived() { }
void foo1() { }
void foo3() { }
} ;int _tmain(int argc, _TCHAR* argv[])
{
Derived * d = (Derived*)new Base() ;
d->foo3() ;
return 0;
}In above code, 1) How it is possible to call foo3() of derived class. 2) If I call d->foo1(), Base class foo1() is getting called. I am not able to understand this. Please explain. Regards msr
Bad line here below. Don't do that.
Derived * d = (Derived*)new Base() ;
Try
Derived * d = new Derived;