Inheritance from base class with reference

This is a discussion on Inheritance from base class with reference within the C++ Programming forums, part of the General Programming Boards category; Hello, I have 3 classes : Tcp_Object Server : public Tcp_Object Server_Client : public Tcp_Object What I want is to ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    69

    Inheritance from base class with reference

    Hello,

    I have 3 classes :
    • Tcp_Object
    • Server : public Tcp_Object
    • Server_Client : public Tcp_Object


    What I want is to be able for calling parent's methods. For ex.
    Code:
    Server_Client::get()
    {
        ((Server)this->parent)->getId();
    }
    How should I do that? I would make constructor to pass reference to object which is calling for that and save it. But what if I create Server object which have no parent?
    • Server_Client has parent of class Server
    • Server object doesn't have any parent


    Here is some of my code so far :

    Code:
    class Tcp_Object
    {
        protected:
            Tcp_Object& _parent;
        public:
            Tcp_Object();
            Tcp_Object(Tcp_Object& parent);
    };
    
    Tcp_Object::Tcp_Object(Tcp_Object& parent): _parent(parent)
    {
        //Looks ok I guess
    }
    
    Tcp_Object::Tcp_Object(): _parent( )
    {
       //But what if I dont have parent and I have to pass init this reference?
    }
    
    Server::Server(int port): Tcp_Object()
    {
       //
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    If it is possible that a Tcp_Object might not have a parent, then you should use a (smart) pointer, not a reference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    So I made it as pointer and now trying like this :

    Code:
    Tcp_Object::Tcp_Object(): _parent(this)
    {
        cout << "construct" << endl;
    }
    
    Server::Server(int port): Tcp_Object()
    {
       //
    }
    It doesnt print nothing from base class constructor, why?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    You are probably not invoking Server::Server(int). Without seeing complete code (class definition, definition of the constructors, and - importantly - your test code) it is difficult to be more specific.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Code:
    class Tcp_Object
    {
        protected:
            Tcp_Object *_parent;
            bool _hasparent;
        public:
            bool hasParent();
            Tcp_Object* parent();
            Tcp_Object();
            Tcp_Object(Tcp_Object *parent);
    };
    
    class Server_Client : public Tcp_Object
    {
        private:
            SOCKET _sock;
            bool _isConnected;
            //threads
            DWORD serve();
        public:
            Server_Client(SOCKET socket, Tcp_Object *parent);
            SOCKET getSock();
    };
    
    class Server : public Tcp_Object
    {
        private:
            int _cap; //server's capacity
            int _port;
            bool _isActive;
            WSADATA _wsa;
            SOCKET _ListenSocket;
            struct addrinfo *result,
                        *ptr,
                        hints;
            boost::thread *_accepthread;
            Server_Clients *clients;
            bool _shutdown;
    
            //methods
            bool initWsa();
            bool createSock();
            bool bindSock();
            bool startListen();
    
            //Threads
            DWORD acceptThread();
        protected:
            //
        public:
            Server(int port);
            virtual ~Server();
            bool start();
            void setCapacity(int c);
            int getCapacity();
    };
    Code:
    Tcp_Object::Tcp_Object()
    {
        this->_hasparent = false;
        cout << "tcp_object w/o parent" << endl;
    }
    
    Tcp_Object::Tcp_Object(Tcp_Object *parent): _parent(parent)
    {
        this->_hasparent = true;
        cout << "tcp_object with parent" << endl;
    }
    
    Server::Server(int port): Tcp_Object()
    {
       //
    }
    Code:
    Server S(1234);
    S.start();

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    That code does not resemble what you previously described. Assuming this last code is accurate, it should be invoking Tcp_Object::Tcp_Object(), not Tcp_Object::Tcp_Object(Tcp_Object *). Given the way your code seems to have changed inconsistently between posts, I would not necessarily assume what you have posted is accurate.
    Right 98% of the time, and don't care about the other 3%.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    69
    Yes I've changed reference to pointer. Constructor without arguments is for situation when object does not have parent.

    Let's say I want to use method Server::getCapacity() within Server_Client object which have Server object as parent.

    Edit: Looks like it works
    Last edited by kargo; 11-09-2011 at 03:56 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting temporary into base class reference
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 12-16-2008, 03:47 AM
  2. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  3. Question on Inheritance (Calling the base class)
    By markcls in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 12:30 AM
  4. Inheritance: assign base class to derived class
    By MWAAAHAAA in forum C++ Programming
    Replies: 15
    Last Post: 01-22-2007, 03:31 PM
  5. Problems w.r.t Virtual Base Class (inheritance)
    By pankajdynamic in forum C++ Programming
    Replies: 1
    Last Post: 04-15-2002, 10:28 AM

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