By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,865 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,865 IT Pros & Developers. It's quick & easy.

Accessing Base class function using a pointer to a derived class

P: n/a
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
Share this Question
Share on Google+
6 Replies


P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a


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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.