473,287 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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::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.
Jul 19 '05 #1
6 5613

"Abhijit Deshpande" <ab***************@lycos.com> wrote in message
news:8d**************************@posting.google.c 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
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::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.
Jul 19 '05 #3

"Craig Thomson" <cr***@craigthomson.me.uk> wrote in message
news:bd**********@news.ukfsn.org...
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
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_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" <jo*************@hotmail.com> wrote in message news:<bd************@ID-196037.news.dfncis.de>...
"Craig Thomson" <cr***@craigthomson.me.uk> wrote in message
news:bd**********@news.ukfsn.org...
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

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_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
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::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 <kb******@gascad.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_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.

Jul 19 '05 #7

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

Similar topics

9
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...
2
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...
10
by: Bhan | last post by:
Using Ptr of derived class to point to base class and viceversa class base { .... } class derived : public base { .... }
2
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...
15
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...
6
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...
2
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...
10
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...
6
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.