useless design pattern

J

John Goche

Hello,

I have come across a class whose structure looks like this:

class Foo {

public:

// ...

private:

FooMarker *fooMarker;

};

class FooMarker {

public:

inline FooMarker(): myStr(0) { }

private:

inline FooMarker(const char *str): myStr(str) { }

friend class Foo;

const char *myStr;

};

----------------------------------------------------

Why not just:

class Foo {

public:

// ...

private:

const char *fooMarker;

};

Seems quite useless to me. Any suggestions?
I would be surprised if such a class were created
just for the sake of adding a class. There must be
some better reason for this.

Thanks,

JG
 
V

Victor Bazarov

John said:
Hello,

I have come across a class whose structure looks like this:

class Foo {

public:

// ...

private:

FooMarker *fooMarker;

};

class FooMarker {

public:

inline FooMarker(): myStr(0) { }

private:

inline FooMarker(const char *str): myStr(str) { }

friend class Foo;

const char *myStr;

};

----------------------------------------------------

Why not just:

class Foo {

public:

// ...

private:

const char *fooMarker;

};

Seems quite useless to me. Any suggestions?
I would be surprised if such a class were created
just for the sake of adding a class. There must be
some better reason for this.

There must be. And since you're not showing how the member of
'Foo' is used (you gave "// ..." there), I can't tell without
making too many wild assumptions. So, I'll refrain.

V
 
D

Daniel T.

John Goche said:
Hello,

I have come across a class whose structure looks like this:

class Foo {

public:

// ...

private:

FooMarker *fooMarker;

};

class FooMarker {

public:

inline FooMarker(): myStr(0) { }

private:

inline FooMarker(const char *str): myStr(str) { }

friend class Foo;

const char *myStr;

};

----------------------------------------------------

Why not just:

class Foo {

public:

// ...

private:

const char *fooMarker;

};

Seems quite useless to me. Any suggestions?
I would be surprised if such a class were created
just for the sake of adding a class. There must be
some better reason for this.

Is "FooMarker" ever mentioned in the public interface of Foo?
 
J

John Goche

Daniel said:
Is "FooMarker" ever mentioned in the public interface of Foo?

Indeed, FooMarker is used in the public interface of Foo. So the
only reason to have this class is to hide implementation details
from the user of the Foo class. There are Foo member functions
to set a FooMarker from a passed in value as well as functions
to do some processing and retrieve corresponding values of
FooMarker. These retrieved values can then be stored on
the user side and possibly passed in to the Foo instance
by the user of the class.

So yes, it is clear that encapsulation in Foo's public
interface is the reason.

Regards,

JG
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top