Advertisement

C++ and 'new'

Started by May 21, 2000 03:09 PM
11 comments, last by Whirlwind 24 years, 7 months ago
VC++ states that ''string'' is not defined and presnts all the other fun errors associated with not knowing what something is. I might need to poke around a bit more and see if there is an STL header file I need to include. There probably is, but VC++ seems to neglect which header files things are in more often than not.

Being full of questions, ''virtual'' allows me to add on to an inherited member function of the parent object, right?

EX:

class someobj
{
private:
int x;
int y;
public:
someobj();
virtual ~someobj();
virutal int addxy();
}

someobj::addxy()
// probably might need an ''int'' out front, but I''ll learn
// either way the hard way
{
return x+y;
}

class notherobj:public someobj
{
private:
int z;
public:
notherobj();
virtual ~notherobj(); // why is this normally virtual?
}

notherobj::addxy()
{
return x+y+z;
}

I''ve got a bit more reading to do to make sure of some things. The book I am reading now ''C++ Inside and Out 2nd Edition'', states that it takes about 9 months to figure out ''virtual''. Inheritance - you get the parents stuff. Polymorphism - you add your own and change the parents stuff. Seems that way.....
To use STL string, you have to include the header string. string is in namespace std, so you have to either have using std::string; in your program text file, or specify std::string everytime you use a string.

If I'm not mistaken, in VC++, if you include string.h, all definitions are placed in global namespace.

The virtual thing makes polymorphism possible. What is polymorphism? In short, it's about having different behaviour using the same method call.

For example, you could have a point2d class, an a derived point3d class. Say you want to draw a point (using a common interface, ie the draw method), be it a point2d or a point3d. The way to do this is to have a virtual draw method in both classes, each with a different implementation. You can then draw any point2d or point3d object by only having a pointer or reference to the base class.

Example:

class point2d
{
public:
virtual void draw() { cout << "point3d::draw()\n"; }
};

class point3d : public point2d
{
public:
virtual void draw() { cout << "point3d::draw()\n"; }
}

point2d p2d;
point3d p3d;
p2d.draw(); // prints "point2d::draw"
p3d.draw(); // prints "point3d::draw"
// now for the 'magic'
point2d* ppoint = &p3d
ppoint->draw(); // what's printed? (it's "point3d::draw")
ppoint = &p2d
ppoint->draw(); // what's printed? (it's "point2d::draw")

If you hadn't declared draw() as virtual, the last method call would execute point2d::draw, but you'd want to have point3d::draw executed.

An example usage for this is the following: suppose you have an array of pointers to point2d. You can then store both point2d objects and point3d objects in the list, and draw all the points using list(i)->draw(). (It should be square brackets, but the forum messes things up).

As for the constructor being virtual, it's essential to have the correct constructor executed when a pointer/reference to a base class is deleted. If the constructor wasn't virtual, the base class constructor would be called, in which case the extra resources any derived class allocates would be leaked.

I hope this clears things up a bit. It's kind of difficult stuff if this is your first time with object orientation, so take it slowly. It isn't for no reason the book discusses this in month 9!

Erik

Edited by - Erik Post on May 22, 2000 4:07:07 PM
Advertisement
virtual destructor - ok got it. I didn''t know assigning a child to an array of parents was what polymorphism allowed, I was thinking it was attributed to inheritance, but now that makes sense. I knew that you could pass a child to a function that called for the parent, I never though of using that approach on such a scale - well I kind of did. I was wondering about the best way to implement the engine''s world object list (scene graph).

I''ve been touching C++ on and off for about (geesh) 6 years, but this is the first time I''ve had a go at doing more than using it as a fancy struct and ''C''ing the rest of the code. C++ keeps looking better for what I want to do all the time. Scary crap here...

I am thinking about using the parent class/object as the base variable/class and passing on children objects as appropriate for the world object list. WOW! Good stuff... so cool...

One day, I''ll take the time to learn something about templates and further explore what is in the STL. I''ll be poking around the web to find a good list of what is supposed to be there and whether it would be worth while to upgrade to VC++ 7.0 (due to v5.0 not having what I need).

I''ll have to try the header file. It would definitely make my file IO easier to use strings for the file name as opposed to char[].

Erik, all, thanks!

This topic is closed to new replies.

Advertisement