Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > OO Programming Question: Global Object in C++

Reply
Thread Tools

OO Programming Question: Global Object in C++

 
 
Paul Stam
Guest
Posts: n/a
 
      08-04-2009

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!
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      08-04-2009
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
 
Reply With Quote
 
 
 
 
Alexander Dong Back Kim
Guest
Posts: n/a
 
      08-05-2009
On Aug 5, 7:34*am, Ian Collins <ian-n...@hotmail.com> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C (functional programming) VS C++ (object oriented programming) Joe Mayo C Programming 168 10-22-2007 01:00 AM
Are global functions methods of the global object? User1014 Javascript 3 12-01-2006 01:02 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57