Thread: Slicing problem with inherited classes.

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148

    Slicing problem with inherited classes.

    Hi there,

    I'm trying to store pointers in a Vector, but the inherited classes datamembers are being lost.

    I believe this is due to Slicing, but I'm unable to see where I'm going wrong, so what hoping someone may be able to point out where my mistake is.

    I've included my programs code. It does compile.

    Header file (diary.h) :
    Code:
    using namespace std;
    #include <string>
    
    class Diary
    {
    public:
    
    	Diary(string mainname, string mainaddress, string  mainpostcode, string maintelno, string maindetails);
    
    private:
    
    protected:
    
    	string m_Name;
    	string m_Address;
    	string m_PostCode;
    	string m_TelNo;
    	string m_Details;
    
    };
    
    Diary::Diary(string mainname, string mainaddress, string  mainpostcode, string maintelno, string maindetails) 
    {
    	m_Name = mainname;
    	m_Address = mainaddress;
    	m_PostCode = mainpostcode;
    	m_TelNo = maintelno;
    	m_Details = maindetails;	
    }
    
    //----------------------------------------------------------------
    class Leads : public Diary
    {
    public:
    
    	Leads(string m_Name, string m_Address, string m_PostCode, string m_TelNo, string m_Details, string m_MainDate, string m_MainTime);
    
    private:
    
    protected:
    
    	string m_MainDate;
    	string m_MainTime;
    
    };
    Leads::Leads(string mainname, string mainaddress, string mainpostcode, string maintelno, string maindetails, string maindate, string maintime) : Diary(mainname, mainaddress, mainpostcode, maintelno, maindetails) 
    {
    	m_MainDate = maindate;
    	m_MainTime = maintime;
    }
    main.cpp :
    Code:
    #include <iostream>
    #include <string>
    #include <vector>
    #include "diary.h"
    
    using namespace std;
    
    int main ()
    {
    	vector<Diary *>vectorname;
    
    	do{
    		int choice = 0;
    		cout << "Choose one of the options below:" << endl;
    		cout << "1) Enter a lead" << endl;
    		cout << "2) Exit the program" << endl;
    		cin >> choice;
    		cin.ignore();
    
    		switch(choice)
    		{
    		case 1:
    			{
    				cout << "You chose to enter a lead" << endl;
    				//---------------------
    				//Collect the leads details
    				//---------------------
    				string mainname;
    				cout << "Name? " << endl;
    				getline (cin, mainname);
    
    				string mainaddress;
    				cout << "Address? " << endl;
    				getline (cin, mainaddress);
    
    				string mainpostcode;
    				cout << "Post Code? " << endl;
    				getline (cin, mainpostcode);
    
    				string maintelno;
    				cout << "Telephone Number? " << endl;
    				getline (cin, maintelno);
    
    				string maindetails;
    				cout << "Details? " << endl;
    				getline (cin, maindetails);
    
    				string maindate;
    				cout << "Date ? DDMMYY " << endl;
    				getline (cin, maindate);
    
    				string maintime;
    				cout << "Time? 24hr HHMM " << endl;
    				getline (cin, maintime);
    
    				cout << "All questions asked" << endl;
    
    				try
    				{
    					Leads *ptr = new Leads(mainname, mainaddress, mainpostcode, maintelno, maindetails, maindate, maintime);
    					vectorname.push_back( ptr );
    				}
    				catch (bad_alloc& ba)
    				{
    					cerr << "bad_alloc caught: " << ba.what() << endl;
    				}
    
    				cout << "past push Lead to list and before deletion" << endl;
    				break;
    			}
    
    		case 2:
    			{
    				cout << "You chose to exit the program" << endl;
    				//save all the objects to the text file
    				//itarate over the vector to delete all the pointers.
    				//exit the program
    				break;
    			}
    		}
    	}
    	while(true);
    
    	system ("PAUSE");
    
    	return 0;
    }
    As always, your help is really appreciated with my queries.

    Thanks a lot.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess the question is how are you determining that slicing is going on, since I don't see you trying to read from the vector here; or for that matter can I determine how you would read from the vector (since you have no "get" functions, nor do you have public variables). If you are trying to access variables from the derived class, you will have to do a reinterpret_cast on the pointer you hold beforehand.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    There is no object slicing in your code, and I cannot see what would lead you to believe that any data members are being lost.
    bit∙hub [bit-huhb] n. A source and destination for information.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by tabstop View Post
    If you are trying to access variables from the derived class, you will have to do a reinterpret_cast on the pointer you hold beforehand.
    Why's that?
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by bithub
    Why's that?
    tabstop probably meant dynamic_cast and/or static_cast rather than reinterpret_cast.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bithub View Post
    Why's that?
    Because C++ has this concept called "types". The type Base* and the type Derived* are not the same.
    Code:
    class Base {
        public: int x;
    };
    
    class Derived : public Base {
        public: int y;
    };
    
    int main() {
        Base *ptr = new Derived;
        ptr->y = 5;
    }
    gives
    Code:
    H:\temp.cpp||In function `int main()':|
    H:\temp.cpp|11|error: 'class Base' has no member named 'y'|
    ||=== Build finished: 1 errors, 1 warnings ===|
    EDIT: Or if your point is as Laserlight says, then never mind. dynamic_cast is what I had in mind.
    Last edited by tabstop; 10-20-2009 at 02:31 PM.

  7. #7
    Registered User
    Join Date
    Aug 2007
    Location
    U.K.
    Posts
    148
    Thanks very much for the help.

    The reason I though Slicing was occurring was because when I viewed Locals in my IDE, the inherited datamembers were not visible in the vector:

    Screen Shot:
    http://i35.tinypic.com/2u8i7w7.jpg

    If slicing is not occurring, is that because I'm storing Base Class pointers?

    If it's normal not to see the inherited datamembers, then that's great. I'm new to this, and so was expecting to see the complete list of datamembers.

    Glad it's looks right, really happy about that!

    Nice one

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having a problem with Classes
    By FoxTrot in forum C++ Programming
    Replies: 10
    Last Post: 09-06-2007, 07:40 PM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. Having a problem with templates and classes
    By bowluswj in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2007, 12:55 PM
  4. problem w/ nested templatized classes
    By *ClownPimp* in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2002, 07:58 AM
  5. Pointers to inherited classes
    By sean in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2001, 03:04 PM