473,326 Members | 2,133 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,326 software developers and data experts.

How to keep the base class pointer in the derived class?

I have a base class vehicle{ }
it has member function vehicle v = fooV (); // it returns base class vehicle
Then I have a derived class car: public vehicle{}
I want to implement a member function
car* fooC()
{
vehicle tempv = fooV();
return static_cast<car*>(&tempv);
}
which return the pointer of derived class car.
I lost the pointer because it call the destructor of vehicle before the '}'.
How can I keep the base class pointer?
Thanks a lot.
Apr 4 '07 #1
6 1631
Banfa
9,065 Expert Mod 8TB
car* fooC()
{
vehicle tempv = fooV();
return static_cast<car*>(&tempv);
}
Hmm it is really not clear to me what you are trying to do. However in this function you instantiate a vehicle class on the stack and then return a pointer to it.

Returning a pointer to any data that is on the stack is a very bad idea because the data is destroyed as the function exits and returns the data to the stack. The data needs to be allocated from the heap (new) or declared statically so that it always exists.

The second bad thing in this code is that you take a pointer to an object of type vehicle and cast it to a pointer of type car. Car is a subclass of vehicle and any given vehicle class reference is not necessarily a reference to an object of type car and in this case definately isn't. If car has extra data members then the pointer you return which is not to an object of type car will be referencing unallocated memory when it tries to access them and will almost certain do something bad (like cause a memory exception).
Apr 4 '07 #2
Thank you for your reply.
My question is:
there is already a class vehicle existed. It has a member function fooV() which returns class vehicle and a lot of similiar functions all return the class itself.
vehicle v = fooV();
The base class vehicle that I can not modify.
I don't want the application to know anything about class vehicle and any member functions of it. But the application can use the class car to do all the same thing as the class vehicle. And all the return values are class car not the vehicle.
To do so I have to develop a new derived class car: public vehicle.
Is there any simple way to do this?
Apr 4 '07 #3
Banfa
9,065 Expert Mod 8TB
Thank you for your reply.
My question is:
there is already a class vehicle existed. It has a member function fooV() which returns class vehicle and a lot of similiar functions all return the class itself.
vehicle v = fooV();
The base class vehicle that I can not modify.
I don't want the application to know anything about class vehicle and any member functions of it. But the application can use the class car to do all the same thing as the class vehicle. And all the return values are class car not the vehicle.
To do so I have to develop a new derived class car: public vehicle.
Is there any simple way to do this?
Expand|Select|Wrap|Line Numbers
  1. vehicle v = fooV();
This is not a member function returning the class unless it is called inside a member function of the class or a derived class.

Are you sure these functions return a class and not a pointer or reference to a class?

you mean
Expand|Select|Wrap|Line Numbers
  1. class vehicle
  2. {
  3. public:
  4.     vehicle fooV(){return *this}
  5.     vehicle(){}
  6.  
  7. }
  8.  
  9. class car : public vehicle
  10. {
  11. public:
  12.     car fooC(){return *this}
  13.     car(){}
  14. }
  15.  
???
Apr 4 '07 #4
Expand|Select|Wrap|Line Numbers
  1. vehicle v = fooV();
This is not a member function returning the class unless it is called inside a member function of the class or a derived class.

Are you sure these functions return a class and not a pointer or reference to a class?

you mean
Expand|Select|Wrap|Line Numbers
  1. class vehicle
  2. {
  3. public:
  4.     vehicle fooV(){return *this}
  5.     vehicle(){}
  6.  
  7. }
  8.  
  9. class car : public vehicle
  10. {
  11. public:
  12.     car fooC(){return *this}
  13.     car(){}
  14. }
  15.  
???
This is what the base class(it is a structure, I deleted some functions) look like:

typedef struct DLLENTRY XMLNode
{
private:

struct XMLNodeDataTag;
XMLNode(struct XMLNodeDataTag *pParent, XMLCSTR lpszName, char isDeclaration);
XMLNode(struct XMLNodeDataTag *p);

public:
XMLNode getParentNode() const; // return the parent node
XMLNode getChildNode(int i=0) const; // return ith child node

private:

typedef struct XMLNodeDataTag // to allow shallow copy and "intelligent/smart" pointers (automatic delete):
{
XMLCSTR lpszName; // Element name (=NULL if root)
int nChild, // Number of child nodes
nText, // Number of text fields
nClear, // Number of Clear fields (comments)
nAttribute; // Number of attributes
char isDeclaration; // Whether node is an XML declaration - '<?xml ?>'
struct XMLNodeDataTag *pParent; // Pointer to parent element (=NULL if root)
XMLNode *pChild; // Array of child nodes
XMLCSTR *pText; // Array of text fields
XMLClear *pClear; // Array of clear fields
XMLAttribute *pAttribute; // Array of attributes
int *pOrder; // order of the child_nodes,text_fields,clear_fields
int ref_count; // for garbage collection (smart pointers)
} XMLNodeData;
XMLNodeData *d;
} XMLNode;


I need a class myXMLNode has function

myXMLNode* mygetparentNode(...)
{
XMLNode mynode = getParentNode() ; // call the base class and return base class

return static_cast<myXMLNode*>(&mynode); // return derived class pointer, if I can return the derived class that will be the best result. Is that possible?
}
Apr 4 '07 #5
Banfa
9,065 Expert Mod 8TB
myXMLNode* mygetparentNode(...)
{
XMLNode mynode = getParentNode() ; // call the base class and return base class

return static_cast<myXMLNode*>(&mynode); // return derived class pointer, if I can return the derived class that will be the best result. Is that possible?
}
I do not believe you can do this, quite apart from the stack problem (you should be returning myXMLNode like the original code) you are trying to cast a pointer to an object to a pointer to something that it is not.

Remember you have been returned an object not a pointer to an object so you have a an object of type XMLNode not an object of type myXMLNode and casting it's pointer to be that is not going to change the objects type.

You could do something like this

Expand|Select|Wrap|Line Numbers
  1. myXMLNode* mygetparentNode(...)
  2. {
  3.   myXMLNode mynode = getParentNode() ; // call the base class and return base class
  4.  
  5. return mynode; // return derived class pointer, if I can return the derived class that will be the best result. Is that possible?
  6. }
  7.  
But you will need to provide an operator= overlod in you myXMLNode class so it can copy the data in an XMLNode. You may well loose data.
Apr 4 '07 #6
I do not believe you can do this, quite apart from the stack problem (you should be returning myXMLNode like the original code) you are trying to cast a pointer to an object to a pointer to something that it is not.

Remember you have been returned an object not a pointer to an object so you have a an object of type XMLNode not an object of type myXMLNode and casting it's pointer to be that is not going to change the objects type.

You could do something like this

Expand|Select|Wrap|Line Numbers
  1. myXMLNode* mygetparentNode(...)
  2. {
  3.   myXMLNode mynode = getParentNode() ; // call the base class and return base class
  4.  
  5. return mynode; // return derived class pointer, if I can return the derived class that will be the best result. Is that possible?
  6. }
  7.  
But you will need to provide an operator= overlod in you myXMLNode class so it can copy the data in an XMLNode. You may well loose data.
Thank you so much!
My another question is because all I need to do is providing similiar(name changed) functions to application and proventing application from knowing anything about the XMLNode, so I used derived class. But derived class can not access private data. Maybe friend class is better for this situation. Should I change it to friend class? Thanks in advance.
Apr 9 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Abhijit Deshpande | last post by:
Is there any elegant way to acheive following: class Base { public: Base() {} virtual ~Base() {} virtual void Method() { cout << "Base::Method called"; return; } };
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...
1
by: ypjofficial | last post by:
Dear All, According to OOPs , a base class pointer can to point to derived class object....call this as fact1 But somehow I am not comfortable while understanding this concept. The explanaition...
6
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
5
by: Scott | last post by:
Hi All, Am I correct in assuming that there is no way to have a base pointer to an object that uses multiple inheritance? For example, class A { /* ... */ }; class B { /* ... */ };
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...
13
by: Rahul | last post by:
Hi Everyone, I was just playing around virtual functions and landed up with the following, class Base1 { public: virtual void sample() { printf("base::sample\n");
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...
0
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...
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: 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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.