Let's say I am creating a tree, and I have a class of Node and a class of Cars.
class Cars : public Node{
....
};
If I have an object all ready defined as a Node, but I now want it to make it a Car as well, how would I do it?
Thank you !
The closest thing you can do is to supply a conversion constructor for your Cars
class and create a new instance:
class Node
{
// stuff goes here
};
class Cars : public Node
{
public:
// other constructors, destructor, members, etc.
Cars(const Node& node)
{
// copy node data into the Node portion of the car
}
};
int main()
{
Node n;
Car c(n);
return 0;
}
If you create it as a Node
first, you cannot magically change it to a Car
without creating an actual Car
instance and copying the data into it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.