OO Programming Question: Global Object in C++

Discussion in 'C++' started by Paul Stam, Aug 4, 2009.

  1. Paul Stam

    Paul Stam Guest

    Hi

    I have probably a quite simple problem but I did not find a proper
    design decision yet. Basically, I have 4 different inherited classes and
    each of those classes has more than 10 methods.

    Each of those classes should make use of the same TCP Socket; this
    object keeps a socket open to the server throughout program execution.
    My idea was to have the TCP obejct declared as "global" so that all
    other classes can use it:

    classTCP TCPSocket;

    class classA
    {
    private:
    public:
    classA();
    virtual void method1();
    ...
    };

    class classB
    {
    private:
    public:
    classB();
    virtual void method1();
    ...
    };

    and so on for classC and classD...

    Unfortunately, when declaring it like this my C++ compiler gives me an
    error message that some initialized data is written in the executable
    (???). So I am wondering if there is any other way I could declare this
    TCP object so that it is available for ALL the other classes and its
    methods? classA() is the first method that will be called when
    initialising this subsystem.

    Many thanks!
     
    Paul Stam, Aug 4, 2009
    #1
    1. Advertising

  2. Paul Stam

    Ian Collins Guest

    Paul Stam wrote:
    >
    > Hi
    >
    > I have probably a quite simple problem but I did not find a proper
    > design decision yet. Basically, I have 4 different inherited classes and
    > each of those classes has more than 10 methods.
    >
    > Each of those classes should make use of the same TCP Socket; this
    > object keeps a socket open to the server throughout program execution.
    > My idea was to have the TCP obejct declared as "global" so that all
    > other classes can use it:
    >
    > classTCP TCPSocket;
    >
    > class classA
    > {
    > private:
    > public:
    > classA();
    > virtual void method1();
    > ...
    > };
    >
    > class classB
    > {
    > private:
    > public:
    > classB();
    > virtual void method1();
    > ...
    > };
    >
    > and so on for classC and classD...
    >
    > Unfortunately, when declaring it like this my C++ compiler gives me an
    > error message that some initialized data is written in the executable
    > (???). So I am wondering if there is any other way I could declare this
    > TCP object so that it is available for ALL the other classes and its
    > methods? classA() is the first method that will be called when
    > initialising this subsystem.


    Have you considered making the TCPSocket instance a singleton? This
    appears to be the pattern you are looking for.

    --
    Ian Collins
     
    Ian Collins, Aug 4, 2009
    #2
    1. Advertising

  3. On Aug 5, 7:34 am, Ian Collins <> wrote:
    > Paul Stam wrote:
    >
    > > Hi

    >
    > > I have probably a quite simple problem but I did not find a proper
    > > design decision yet. Basically, I have 4 different inherited classes and
    > > each of those classes has more than 10 methods.

    >
    > > Each of those classes should make use of the same TCP Socket; this
    > > object keeps a socket open to the server throughout program execution.
    > > My idea was to have the TCP obejct declared as "global" so that all
    > > other classes can use it:

    >
    > > classTCP TCPSocket;

    >
    > > class classA
    > > {
    > >     private:
    > >     public:
    > >     classA();
    > >     virtual void method1();
    > >     ...
    > > };

    >
    > > class classB
    > > {
    > >     private:
    > >     public:
    > >     classB();
    > >     virtual void method1();
    > >     ...
    > > };

    >
    > > and so on for classC and classD...

    >
    > > Unfortunately, when declaring it like this my C++ compiler gives me an
    > > error message that some initialized data is written in the executable
    > > (???). So I am wondering if there is any other way I could declare this
    > > TCP object so that it is available for ALL the other classes and its
    > > methods? classA() is the first method that will be called when
    > > initialising this subsystem.

    >
    > Have you considered making the TCPSocket instance a singleton?  This
    > appears to be the pattern you are looking for.
    >
    > --
    > Ian Collins


    Hi Paul,

    As Ian said, using the singleton approach for the TCPSocket would be a
    good way of using it. However, you may eventually want to have more
    than one TCPSocket. For that case, I would think passing TCPSocket
    instance pointer to the classes when the instances of the classes are
    created (possibly by using constructor). In that way, you can share a
    instance between different classes.

    Cheers,
    Alex Kim
     
    Alexander Dong Back Kim, Aug 5, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    1
    Views:
    792
    Alvin Bruney
    Jul 29, 2003
  2. Wayne
    Replies:
    2
    Views:
    476
    Wayne
    Nov 11, 2003
  3. Felix Kater
    Replies:
    3
    Views:
    389
    Toby A Inkster
    Feb 13, 2007
  4. Joe Mayo
    Replies:
    168
    Views:
    3,326
    David Thompson
    Oct 22, 2007
  5. User1014
    Replies:
    3
    Views:
    192
    Richard Cornford
    Dec 1, 2006
Loading...

Share This Page