Thread: Inheritance from base class with reference

  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
    28,413
    If it is possible that a Tcp_Object might not have a parent, then you should use a (smart) pointer, not a reference.
    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

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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,815
    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%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  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 04: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, 04: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, 04: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