Design problem

J

Juan Pablo

Hi.
I have 3 objects. Object INTERFACEDAT will interact with plain text
files and retrieve some kind of recordset/collection, a FIELDS object,
which will be populated with FIELD objects. From the main module I
want to retrieve a collection of FIELDS so I need to include the 3
classes. This seems good because main needs to know these 3 classes.
But how can avoid, for example, creating an instance of FIELDS
directly in main module, I mean, I want only INTERFACEDAT to create
this kind of object. The same applies to FIELD object. How can I
restrict the creation of these to objects to INTERFACEDAT, how can
avoid from main doing this (field* f = new field;)?
Thank you in advance

Juan Pablo Polotto
 
K

Karl Heinz Buchegger

Juan said:
Hi.
I have 3 objects. Object INTERFACEDAT will interact with plain text
files and retrieve some kind of recordset/collection, a FIELDS object,
which will be populated with FIELD objects. From the main module I
want to retrieve a collection of FIELDS so I need to include the 3
classes. This seems good because main needs to know these 3 classes.
But how can avoid, for example, creating an instance of FIELDS
directly in main module, I mean, I want only INTERFACEDAT to create
this kind of object. The same applies to FIELD object. How can I
restrict the creation of these to objects to INTERFACEDAT, how can
avoid from main doing this (field* f = new field;)?
Thank you in advance

By making the constructor of FIELD private or protected.
Nobody can then create an object of that class type, except
an INTERFACEDAT object, which you declare to be a friend of FIELD.
 
J

jeffc

Juan Pablo said:
Hi.
I have 3 objects. Object INTERFACEDAT will interact with plain text
files and retrieve some kind of recordset/collection, a FIELDS object,
which will be populated with FIELD objects. From the main module I
want to retrieve a collection of FIELDS so I need to include the 3
classes. This seems good because main needs to know these 3 classes.
But how can avoid, for example, creating an instance of FIELDS
directly in main module, I mean, I want only INTERFACEDAT to create
this kind of object. The same applies to FIELD object. How can I
restrict the creation of these to objects to INTERFACEDAT, how can
avoid from main doing this (field* f = new field;)?

If you really want to restrict it, with code, then you can make the
constructor private. Then you can make INTERFACEDAT a friend of FIELD.
 
J

Juan Pablo

Karl Heinz Buchegger said:
By making the constructor of FIELD private or protected.
Nobody can then create an object of that class type, except
an INTERFACEDAT object, which you declare to be a friend of FIELD.

Thank you all for your quick responses.
 

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

Similar Threads

Problem with codewars. 5
Hello , Im Emilio 1
A problem in viewing the output! 2
Design Problem 2 2
A design question .... 1
Read / Write Pattern Design 4
Good design question 2
A Design Problem 6

Members online

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top