Changing a derived class to a base class?

This is a discussion on Changing a derived class to a base class? within the C++ Programming forums, part of the General Programming Boards category; Well I'm not sure that's exactly what I'm doing. I have a base class called Space and several derived classes, ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    56

    Changing a derived class to a base class?

    Well I'm not sure that's exactly what I'm doing. I have a base class called Space and several derived classes, including PassiveSpace. I want to take advantage of virtual functions so that if I have an array of spaces and call
    Code:
    theSpaces[ someNum ].action()
    it calls the appropriate function. My problem is in actually creating the array to begin with. I have something like this:
    Code:
    Space * itsSpaces = new Space[ 40 ];
    :
    : // later
    :
    itsSpaces[0] = new PassiveSpace( /* appropriate constructor args go here */ );
    itsSpaces[1] = new PassiveSpace( /* ditto */ );
    :
    : // etc
    But I got an error regarding "no operator= defined for right-hand operand of type class Property *". So I went ahead and defined it:
    Code:
    PassiveSpace& Space::operator=( PassiveSpace * thePass ) {
    	delete this;
    	return *thePass;
    }
    Now it compiles OK, but I'm getting runtime assertion failures that I am pretty sure are a result of this operation. My question is - is that operator= legal? Or is there some better way to do it? Any help would be welcome....thanks in advance!

  2. #2
    Shadow12345
    Guest
    do you realize the new keyword calls the constructor?

    if you want to know what I would do

    EDIT:
    this seems like a good scenario using vectors, with vectors you can call the appropriate constructor and all that fun stuff

    look at this example and tell me if it helps in any way

    Code:
    #include <iostream>
    #include <conio.h>
    #include <vector>
    
    using namespace std;
    
    class pinky{
    public:
    	pinky(int);
    	int GetAge() {
    		return age;
    	}
    private:
    	int age;
    };
    
    pinky::pinky(int itsage) {
    age = itsage;
    }
    
    vector<pinky*> pinkies;
    
    int main(void) {
    int numpinkies;
    int pinkyage;
    	cout << "How many pinkies do you want created" << endl;
    	cin >> numpinkies;
    	for(int index = 0; index < numpinkies; index++) {
    		cout << "Enter an age for pinky number " << (index + 1) << endl;
    		cin >> pinkyage;
    		pinkies.push_back(new pinky(pinkyage));
    		cout << "Pinky number " << (index + 1) << "'s age is " << pinkies[index]->GetAge() << endl;
    	}
    
    	return 0;
    }
    i dunno if you've ever seen vectors or what or if im even addressing the problem. if im not helping i suck.
    Last edited by Shadow12345; 12-10-2002 at 04:31 PM.

  3. #3
    Shadow12345
    Guest
    Another way you could do much the same thing is have a vector of values instead of a vector of pointers, and instead of calling new just call the type with the constructor. I put this in another post so it wouldn't seem like so much.

    they're basically the same and they do exactly the same thing

    Code:
    #include <iostream>
    #include <conio.h>
    #include <vector>
    
    using namespace std;
    
    class pinky{
    public:
    	pinky(int);
    	int GetAge() {
    		return age;
    	}
    private:
    	int age;
    };
    
    pinky::pinky(int itsage) {
    age = itsage;
    }
    
    vector<pinky> pinkies;
    
    int main(void) {
    int numpinkies;
    int pinkyage;
    	cout << "How many pinkies do you want created" << endl;
    	cin >> numpinkies;
    	for(int index = 0; index < numpinkies; index++) {
    		cout << "Enter an age for pinky number " << (index + 1) << endl;
    		cin >> pinkyage;
    		pinkies.push_back(pinky(pinkyage));
    		cout << "Pinky number " << (index + 1) << "'s age is " << pinkies[index].GetAge() << endl;
    	}
    
    	return 0;
    }
    Last edited by Shadow12345; 12-10-2002 at 04:40 PM.

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    I hadn't ever used the vector class before, but I did some research and incorporated it into my program. However, I do not see how it solves the problem, and now I have the problem that the subscript operator on vector apparently returns a constant reference to the object, which means I can't edit it. This will cause problems...

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    56
    Sorry for all the fuss....I've figured out the problem. I needed to say:
    Code:
    Space * foo = new PassiveSpace( /* blah */ );
    and not:
    Code:
    PassiveSpace * foo = new PassiveSpace( /* blah */);
    in order for it to work right. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. base class pointer pointing at derived class
    By mynickmynick in forum C++ Programming
    Replies: 11
    Last Post: 12-01-2008, 11:26 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 03:31 PM
  4. finding derived class type of a pointer to a base class
    By LinuxCoder in forum C++ Programming
    Replies: 15
    Last Post: 04-10-2006, 11:08 AM
  5. Replies: 1
    Last Post: 12-11-2002, 09:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21