Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Accessing Base class function using a pointer to a derived class

Reply
Thread Tools

Accessing Base class function using a pointer to a derived class

 
 
Abhijit Deshpande
Guest
Posts: n/a
 
      06-28-2003
Is there any elegant way to acheive following:

class Base {
public:
Base() {}
virtual ~Base() {}
virtual void Method() { cout << "Base::Method called"; return;
}
};

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Method called"; return; }
};

int main() {
Derived deriveObj;
Base * basePtr = 0;

basePtr = <some kind of cast????> &deriveObj;

basePtr->Method();
}

In the above code, the call "basePtr->Method" should print
"Base::Method called" and not "Derived::Method called".

Is there any way to assign address of "deriveObj" to "basePtr" so that
the virtual mechanism is bypassed and call to member function "Method"
actually calls the member function from the "class Base" and not from
"class Derived".

Thanks in advance,
Regards,
Abhijit.
 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      06-28-2003
Abhijit Deshpande wrote:

> Is there any elegant way to acheive following:
>
> class Base {
> public:
> Base() {}
> virtual ~Base() {}
> virtual void Method() { cout << "Base::Method called"; return;
> }
> };
>
> class Derived : public Base {
> public:
> Derived() {}
> ~Derived()
> void Method() { cout << "Derived::Method called"; return; }
> };
>
> int main() {
> Derived deriveObj;
> Base * basePtr = 0;
>
> basePtr = <some kind of cast????> &deriveObj;
>
> basePtr->Method();
> }
>
> In the above code, the call "basePtr->Method" should print
> "Base::Method called" and not "Derived::Method called".
>
> Is there any way to assign address of "deriveObj" to "basePtr" so that
> the virtual mechanism is bypassed and call to member function "Method"
> actually calls the member function from the "class Base" and not from
> "class Derived".


basePtr->Base::Method();

But please think thrice before doing that. Looks like bad design to me.
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      06-28-2003

"Abhijit Deshpande" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> Is there any elegant way to acheive following:
>
> class Base {
> public:
> Base() {}
> virtual ~Base() {}
> virtual void Method() { cout << "Base::Method called"; return;
> }
> };
>
> class Derived : public Base {
> public:
> Derived() {}
> ~Derived()
> void Method() { cout << "Derived::Method called"; return; }
> };
>
> int main() {
> Derived deriveObj;
> Base * basePtr = 0;
>
> basePtr = <some kind of cast????> &deriveObj;
>
> basePtr->Method();
> }
>
> In the above code, the call "basePtr->Method" should print
> "Base::Method called" and not "Derived::Method called".
>
> Is there any way to assign address of "deriveObj" to "basePtr" so that
> the virtual mechanism is bypassed and call to member function "Method"
> actually calls the member function from the "class Base" and not from
> "class Derived".
>
> Thanks in advance,
> Regards,
> Abhijit.


You can do this

Derived deriveObj;
Base * basePtr = 0;

basePtr = &deriveObj;

basePtr->Base::Method(); // calls Base::Method

but there is no way to bypass the virtual machanism when you assign a
pointer.

john


 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-28-2003

"Craig Thomson" <(E-Mail Removed)> wrote in message
news:bdklpg$4r6$(E-Mail Removed)...
> Hi,
>
> > Is there any way to assign address of "deriveObj" to "basePtr" so that
> > the virtual mechanism is bypassed and call to member function "Method"
> > actually calls the member function from the "class Base" and not from
> > "class Derived".

>
> I'm a little hazy on the whole casting business, but as I understand it

what
> you want is:
>
> main() {
> Derived deriveObj;
> Base * basePtr = 0;
>
> basePtr = dynamic_cast<Base*>(&DeriveObj);
>
> basePtr->Method();
> }
>
> or
>
> main() {
> Derived deriveObj;
> Base * basePtr = 0;
>
> basePtr = static_cast<Base*>(&DeriveObj);
>
> basePtr->Method();
> }
>
> Dynamic cast is safer because it does run time type checking but as long

as
> your going from derived to base class static cast should work fine too.
> Google for dynamic_cast or static_cast and you should get all the
> information you need.
>


Neither type of cast is necessary when converting from a derived class
pointer to a base class pointer, and neither cast achieves what the OP wants
which is to override the virtual function calling mechanism.

You've got this mixed up with converting from a base class pointer to a
derived class pointer, when some sort of cast is necessary.

john


 
Reply With Quote
 
Abhijit Deshpande
Guest
Posts: n/a
 
      06-30-2003
Thanks John and Ralf for the solution.

But I was wondering, should following piece of code work?
In addition to "class Base" and "class Derived", we define one more
class,

class DummyBase() {

public:

DummyBase() {
}

~DummyBase() {
}

void Method() {
Base::Method();

return;
}
};

int main() {
Derived deriveObj;
DummyBase * dummyBasePtr = reinterpret_cast<DummyBase
*>(&deriveObj);

dummyBasePtr->Method();

return 0;
}

This should print "Base::Method called". Is there anything
conceptually wrong in above piece of code?? Because, I tried it using
GNU g++ on RedHat linux 7.2, and it still prints "Derived::Method
called".

Regards,
Abhijit.

"John Harrison" <(E-Mail Removed)> wrote in message news:<bdkjor$u82sv$(E-Mail Removed)>...
> "Craig Thomson" <(E-Mail Removed)> wrote in message
> news:bdklpg$4r6$(E-Mail Removed)...
> > Hi,
> >
> > > Is there any way to assign address of "deriveObj" to "basePtr" so that
> > > the virtual mechanism is bypassed and call to member function "Method"
> > > actually calls the member function from the "class Base" and not from
> > > "class Derived".

> >
> > I'm a little hazy on the whole casting business, but as I understand it

> what
> > you want is:
> >
> > main() {
> > Derived deriveObj;
> > Base * basePtr = 0;
> >
> > basePtr = dynamic_cast<Base*>(&DeriveObj);
> >
> > basePtr->Method();
> > }
> >
> > or
> >
> > main() {
> > Derived deriveObj;
> > Base * basePtr = 0;
> >
> > basePtr = static_cast<Base*>(&DeriveObj);
> >
> > basePtr->Method();
> > }
> >
> > Dynamic cast is safer because it does run time type checking but as long

> as
> > your going from derived to base class static cast should work fine too.
> > Google for dynamic_cast or static_cast and you should get all the
> > information you need.
> >

>
> Neither type of cast is necessary when converting from a derived class
> pointer to a base class pointer, and neither cast achieves what the OP wants
> which is to override the virtual function calling mechanism.
>
> You've got this mixed up with converting from a base class pointer to a
> derived class pointer, when some sort of cast is necessary.
>
> john

 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      06-30-2003


Abhijit Deshpande wrote:
>
> Thanks John and Ralf for the solution.
>
> But I was wondering, should following piece of code work?
> In addition to "class Base" and "class Derived", we define one more
> class,
>
> class DummyBase() {


>
> public:
>
> DummyBase() {
> }
>
> ~DummyBase() {
> }
>
> void Method() {
> Base::Method();
>
> return;
> }
> };
>
> int main() {
> Derived deriveObj;
> DummyBase * dummyBasePtr = reinterpret_cast<DummyBase
> *>(&deriveObj);
>


You are casting way to much!
What (if any) is the relationship of Derived and DummyBase.
Please show it with code and not with english descriptions.


> dummyBasePtr->Method();
>
> return 0;
> }
>
> This should print "Base::Method called". Is there anything
> conceptually wrong in above piece of code??


Impossible to say without seeing the actual, complete code you used to test it.


--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Abhijit Deshpande
Guest
Posts: n/a
 
      07-01-2003
Well, here is the complete piece of code, I tried..

class Base {

public:

Base() {
};

virtual ~Base() {
};

virtual void Method() {
printf("Base::Method called.\n");

return;
}
};

class Derived : public Base {

public:

Derived() {
};

~Derived() {
};

void Method() {
printf("Derived::Method called.\n");

return;
}
};

class DummyBase : public Base {

public:

DummyBase() {
}

~DummyBase() {
}

void Method() {
Base::Method();

return;
}
};

int main() {

Derived derivedObj;
DummyBase *dummyBasePtr;

dummyBasePtr = reinterpret_cast<DummyBase *>(&derivedObj);

dummyBasePtr->Method();
}

And the expected output is "Base::Method called.", whereas the actual
output is "Derived::Method called."

Regards,
Abhijit.

Karl Heinz Buchegger <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Abhijit Deshpande wrote:
> >
> > Thanks John and Ralf for the solution.
> >
> > But I was wondering, should following piece of code work?
> > In addition to "class Base" and "class Derived", we define one more
> > class,
> >
> > class DummyBase() {

>
> >
> > public:
> >
> > DummyBase() {
> > }
> >
> > ~DummyBase() {
> > }
> >
> > void Method() {
> > Base::Method();
> >
> > return;
> > }
> > };
> >
> > int main() {
> > Derived deriveObj;
> > DummyBase * dummyBasePtr = reinterpret_cast<DummyBase
> > *>(&deriveObj);
> >

>
> You are casting way to much!
> What (if any) is the relationship of Derived and DummyBase.
> Please show it with code and not with english descriptions.
>
>
> > dummyBasePtr->Method();
> >
> > return 0;
> > }
> >
> > This should print "Base::Method called". Is there anything
> > conceptually wrong in above piece of code??

>
> Impossible to say without seeing the actual, complete code you used to test it.

 
Reply With Quote
 
samualmenon samualmenon is offline
Junior Member
Join Date: Jul 2008
Posts: 1
 
      07-16-2008
Since your Base class "Method" is virtual its going to call the derived method and expected output will be Derived method and not the base Method. Try removing virtual from Base Method and you should see what you want to see.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
access public function of derived class through base class pointer Banaticus Bart C++ 9 05-07-2004 12:34 AM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments