Displaying pointer's actual values in inheritance

This is a discussion on Displaying pointer's actual values in inheritance within the C++ Programming forums, part of the General Programming Boards category; Basically the problem goes like this... I have a parent class called Head, and a derived class ( from Head ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    8

    Question Displaying pointer's actual values in inheritance

    Basically the problem goes like this...

    I have a parent class called Head, and a derived class ( from Head ) called Arms.

    Head *MyHead; // Pointer
    Arms MyArms;

    I have overloaded the operator << to handle file output, so when I do "outfile << MyArms", it is supposed to output it's variables into whatever outfile is.

    MyHead = &MyArms;

    Now I need to use the same overloaded operator ( << ) on MyHead, but what it outputs is the memory address of MyArms, instead of what is specified in the code of the overloaded << ( which is supposed to output its variables ).

    Any help will be greatly appreciated, thank you!

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    How are you going about printing it out just like this
    Code:
    <<MyHead
    Woop?

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    The code in << overloading goes like this

    Code:
    ofstream &operator << (ofstream &out, Arms MyArms)
    {
             out << MyArms.size << MyArms.Length;
    }

  4. #4
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    try doing this on my head
    Code:
    out<<myHead->size<<myHead->Length;
    Woop?

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    Actually my project requires me to make use of the overloaded operator to output the variables, so I pretty much must use
    Code:
    outfile << MyHead
    And the actual code to display the variables will have to go into the overloaded operator.

    Sorry for not mentioning this, but in fact there is actually another child class ( derived from Head ) that has its own different set of variables..

    So when I do
    Code:
    outfile << MyHead
    , it should be able to identify whether it is point to an Arms object or the other ( lets call it Legs ), and use the corresponding overloaded operator ( Legs has << overloaded too, for the same purpose ).

  6. #6
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    could i possibly see your 3 classes ?
    Woop?

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    Code:
    class Head
    {
    public:
    constructors etc;
    
    protected:
    int size;
    int length;
    char name[21];
    };
    
    class Arms : public Head
    {
    friend bah bah // overloading the << operator specifically for Arms
    public :
    constructs etc;
    
    private:
    int number;
    int somevar;
    };
    
    class Legs : public Head
    {
    
    friend bah bah // overloading the << operator specifically for Legs
    public:
    constructors etc;
    
    private :
    int anothervar;
    int yetsomevar;
    };
    I have 3 arrays in fact,

    Code:
    Head * MyHeads[20];
    Arms MyArms[20];
    Legs MyLegs[20];
    Data are read into MyArms and MyLegs with respective expected inputs, and then MyHeads pointer array is used to point to each and every one of the initalised cells of MyArms and MyLegs...

    So what I meant was,
    MyHeads[0] = & MyArms[0];
    MyHeads[1] = & MyLegs[0];

    The point was to combine both MyArms and MyLegs into one array, and sort them by name.

    So now I have MyHeads filled with pointers to MyArms and MyLegs objects ( sorted ), and I want to output the sorted list into a file.

    Thus if I were to go

    Code:
    for( int x = 0; x < 20; x++) // assuming all 20 slots are filled
    {
    outfile << MyHeads[x];
    }
    If i'm not wrong, what the loop does it to call the corresponding overloaded << ( Arms or Legs ), which then in the overloaded << it'll display its variables into a file..

    But instead, it outputs the memory address of the pointed object.
    Last edited by Poof; 07-29-2004 at 05:11 AM.

  8. #8
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    so basically you need to out put the my head's array
    try this instead
    Code:
    for(int i = 0,i<20;i++)
    {
      out<<*MyHeads[i];
    }
    Last edited by prog-bman; 07-29-2004 at 05:14 AM.
    Woop?

  9. #9
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    I get this error

    binary '<<' : no operator defined which takes a right-hand operand of type 'class Head' ( or there is no acceptable conversion )

    If i'm not completely mistaken, it is looking under Head for the overloaded operator, instead of the subclass Arms or Legs depending on what the current cell is pointing to. Is there anyway to get around this?
    Last edited by Poof; 07-29-2004 at 05:17 AM.

  10. #10
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Hrm its late(probably why im not much of help) try this one more thing than im going
    Code:
    for(int i=0;i<20;i++)
    {
       out<<myHead[i]->overLoadedOperatorFunction
    }
    Woop?

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    Tried

    Code:
    out << MyHead[x]->operator<<; // << is not a member of Head
    out << MyHead[x]-><<; // Syntax error
    Your help is greatly appreciated prog-bman, even if it doesn't work out just yet, you have my most sincere gratitude = )
    Last edited by Poof; 07-29-2004 at 05:34 AM.

  12. #12
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,802
    Can you possibly put your overloaded operator in you base class and then it could be derived from your child classes?
    Woop?

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    That, unfortunately, will be against the requirements of my project

    Each class ( derived or not ) has its own specified set of member functions, variables and overloaded operators. I am not allowed to add / modify those

    Btw please check your inbox Prog-Bman!
    Last edited by Poof; 07-29-2004 at 06:37 AM.

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    This test application works for me... you'll need to enable RTTI in your project settings for it to work:

    Code:
    #include <iostream>
    using namespace std;
    
    class Head
    {
    public:
        virtual ~Head() {}
    protected:
        int size;
        int length;
        char name[21];
    };
    
    class Arms : public Head
    {
    public:
        friend ostream& operator<< (ostream& os, const Arms& AnArm);
        Arms()
        {
            number = 10;
            somevar = 30;
            size = 50;
            length = 70;
        }
    private:
        int number;
        int somevar;
    };
    
    ostream& operator<< (ostream& os, const Arms& AnArm)
    {
        os << "Arm: number(" << AnArm.number
           << "), somevar(" << AnArm.somevar
           << "), size(" << AnArm.size
           << "), length(" << AnArm.length
           << ")" << endl;
        return os;
    }
    
    class Legs : public Head
    {
    public:
        friend ostream& operator<< (ostream& os, const Legs& ALeg);
        Legs()
        {
            anothervar = 20;
            yetsomevar = 40;
            size = 60;
            length = 80;
        }
    private:
        int anothervar;
        int yetsomevar;
    };
    
    ostream& operator<< (ostream& os, const Legs& ALeg)
    {
        os << "Leg: anothervar(" << ALeg.anothervar
           << "), yetsomevar(" << ALeg.yetsomevar
           << "), size(" << ALeg.size
           << "), length(" << ALeg.length
           << ")" << endl;
        return os;
    }
    
    int main()
    {
        Head* MyHeads[10];
        Arms MyArms[5];
        Legs MyLegs[5];
    
        // Alternately assign Arms ptr then Legs ptr to MyHeads array
    
        for( int x = 0, i = 0; x < 10; ++x )
        {
            if( !(x % 2) ) MyHeads[x] = &MyArms[i]; // Even MyHeads gets an arm
            else MyHeads[x] = &MyLegs[i++];         // Odd MyHeads gets a leg
        }
    
        // Now print them out
    
        for( int y = 0; y < 10; ++y )
        {
            Arms* Arm = dynamic_cast<Arms*>(MyHeads[y]);
            Legs* Leg = dynamic_cast<Legs*>(MyHeads[y]);
            if( Arm ) cout << *Arm;
            else cout << *Leg;
        }
    
        return 0;
    }
    Barring any typos, this should output:
    Code:
    Arm: number(10), somevar(30), size(50), length(70)
    Leg: anothervar(20), yetsomevar(40), size(60), length(80)
    Arm: number(10), somevar(30), size(50), length(70)
    Leg: anothervar(20), yetsomevar(40), size(60), length(80)
    Arm: number(10), somevar(30), size(50), length(70)
    Leg: anothervar(20), yetsomevar(40), size(60), length(80)
    Arm: number(10), somevar(30), size(50), length(70)
    Leg: anothervar(20), yetsomevar(40), size(60), length(80)
    Arm: number(10), somevar(30), size(50), length(70)
    Leg: anothervar(20), yetsomevar(40), size(60), length(80)
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  15. #15
    Registered User
    Join Date
    Jul 2003
    Posts
    8
    Oh my god... That worked like a charm!!

    Thank you so much hk_mp5hkpw and prog-bman, I greatly appreciate your help!!!

    *dance*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with pointers to an abstract class and inheritance
    By bainevii in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2009, 07:51 AM
  2. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 06:50 PM
  3. Replies: 16
    Last Post: 10-03-2008, 04:35 PM
  4. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  5. Method pointers and inheritance
    By Magos in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2006, 06:43 PM

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