473,748 Members | 10,028 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing Base class function using a pointer to a derived class

Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Metho d 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::Meth od called" and not "Derived::Metho d 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.
Jul 19 '05 #1
6 5637

"Abhijit Deshpande" <ab************ ***@lycos.com> wrote in message
news:8d******** *************** ***@posting.goo gle.com...
Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Metho d 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::Meth od called" and not "Derived::Metho d 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
Jul 19 '05 #2
Abhijit Deshpande wrote:
Is there any elegant way to acheive following:

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

class Derived : public Base {
public:
Derived() {}
~Derived()
void Method() { cout << "Derived::Metho d 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::Meth od called" and not "Derived::Metho d 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.
Jul 19 '05 #3

"Craig Thomson" <cr***@craigtho mson.me.uk> wrote in message
news:bd******** **@news.ukfsn.o rg...
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<Ba se*>(&DeriveObj );

basePtr->Method();
}

or

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

basePtr = static_cast<Bas e*>(&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
Jul 19 '05 #4
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_cas t<DummyBase
*>(&deriveObj) ;

dummyBasePtr->Method();

return 0;
}

This should print "Base::Meth od 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::Metho d
called".

Regards,
Abhijit.

"John Harrison" <jo************ *@hotmail.com> wrote in message news:<bd******* *****@ID-196037.news.dfn cis.de>...
"Craig Thomson" <cr***@craigtho mson.me.uk> wrote in message
news:bd******** **@news.ukfsn.o rg...
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<Ba se*>(&DeriveObj );

basePtr->Method();
}

or

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

basePtr = static_cast<Bas e*>(&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

Jul 19 '05 #5


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_cas t<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::Meth od 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
kb******@gascad .at
Jul 19 '05 #6
Well, here is the complete piece of code, I tried..

class Base {

public:

Base() {
};

virtual ~Base() {
};

virtual void Method() {
printf("Base::M ethod 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_cas t<DummyBase *>(&derivedObj) ;

dummyBasePtr->Method();
}

And the expected output is "Base::Meth od called.", whereas the actual
output is "Derived::Metho d called."

Regards,
Abhijit.

Karl Heinz Buchegger <kb******@gasca d.at> wrote in message news:<3F******* ********@gascad .at>...
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_cas t<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::Meth od 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.

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
4995
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
2
2310
by: Steven T. Hatton | last post by:
I find the surprising. If I derive Rectangle from Point, I can access the members of Point inherited by Rectangle _IF_ they are actually members of a Rectangle. If I have a member of type Point in Rectangle, the compiler tells me Point::x is protected. I would have expected Rectangle to see the protected members of any Point. Compiling the following code give me this error: g++ -o rectangle main.cc main.cc: In member function `size_t...
10
3330
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
2
2637
by: Jessica | last post by:
I have a base class and a derived class, but I am getting errors when I try to access functions of the derived class. Simplified version of my code is as follows: //////////////// // test2.hh class BaseClass {
15
3532
by: Juha Nieminen | last post by:
I'm sure this is not a new idea, but I have never heard about it before. I'm wondering if this could work: Assume that you have a common base class and a bunch of classes derived from it, and you want to make a deque which can contain any objects of any of those types. Normally what you would have to do is to make a deque or vector of pointers of the base class type and then allocate each object dynamically with 'new' and store the...
6
4599
by: Me | last post by:
I need to be able to acces non-virtual members of sublcasses via a base class pointer...and without the need for an explicit type cast. I thought a pure virtual getPtr() that acts as a type cast would solve the problem, but it appears not to. I need this functionality to make object serialization a reality. Having to explicitly cast each deserialized object to its original type defeats the purpose of my serializing the blasted things in...
2
2412
by: cmonthenet | last post by:
Hello, I searched for an answer to my question and found similar posts, but none that quite addressed the issue I am trying to resolve. Essentially, it seems like I need something like a virtual static function (which I know is illegal), but, is there a way to provide something similar? The class that is the target of my inquiry is a template class that interfaces to one of several derived classes through a pointer to a base class. The...
10
3599
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the derived type 3 - this function then 'grows' the derived type object (by pushing onto a vector).
6
8159
by: Bhawna | last post by:
I am into c++ code maintenance for last 3-4 years but recently I am put into design phase of a new project. Being a small comapany I dont have enough guidance from seniors. Currently I am into a situation where I am implementing base class functions by including a pointer to subclass member in base class. Reason being functionality is common for subclasses but the members are common within subclass only (static member of subclass) but...
0
8984
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9363
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9312
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9238
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8237
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4593
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4864
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.