LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   automatic(dynamic ) Instantiating in c++ (https://www.linuxquestions.org/questions/programming-9/automatic-dynamic-instantiating-in-c-760959/)

naveenisback 10-10-2009 06:31 AM

automatic(dynamic ) Instantiating in c++
 
Hi to all,

language : C++
Os:Linux

I have an example but i dont kno how to implement it in c++.


Ex:
Suppose I have a System and have many devices (keyboard, printer, scanner network like this) are connected to it. So I can use either composition or Inheritance. but for my application Inheritance is suitable. But my problem is If I more than 1 printer or any device connected dynamically.. that time how can instantiate it dynamically.

Note that System is a class and Printer is derived from it.

class Printer: public System
{
char* printertype;
char* printervendor;
Printer* Printerptr (or Printer Printerobj);

public:
//some methods//
};

Quote:

Printer* Printerptr (or Printer Printerobj);
Is this is way to create multi printer objects..
If this is correct way, which one is good.?

johnsfine 10-10-2009 06:51 AM

Try some easier problems first.

I'm making that suggestion as a result of looking at several of your other threads.

Your question in this thread apparently demonstrates a lack of understanding of the basic concepts of classes in C++.

I don't think you will get that understanding attacking problems that are way beyond your C++ knowledge.

Find a C++ tutorial. Maybe you can skip the really basic parts, but when you get to the part about classes, go through that in detail. I think you also need to work through some examples to get an understanding of pointers and references.

rjlee 10-10-2009 10:15 AM

Quote:

Originally Posted by naveenisback (Post 3714458)
class Printer: public System
{
char* printertype;
char* printervendor;
Printer* Printerptr (or Printer Printerobj);

public:
//some methods//
};


Is this is way to create multi printer objects..
If this is correct way, which one is good.?

That looks wrong to me; if, as you say, a Printer is connected to a System then it probably shouldn't be inheriting from it.

The first line of your code says that a Printer is a System, i.e. a Printer provides everything that a System can do and more. And every time you create a new Printer, you are essentially creating a new System.

What you probably want is for the Printer class to have a reference to a System. If you have multiple devices, of which Printer is one, then you should have Printer inherit from Device, which should in turn have a reference to the System.

This assumes that the Printer needs to know about the System at all; you haven't said why you need a System class. Unless you need it, leave it out.

It may also make sense for the System to have a vector of Printer or Device objects, particularly if you want to cope with networked printers shared between systems (or you could have one Printer object per printer per system).

You can't put an instance of a Printer inside a Printer object; you would get an infinite loop in construction. You could put a pointer reference to another Printer but it's hard to see why you'd want to. For a linked list of printers, use a separate list<Printer> object.


All times are GMT -5. The time now is 01:32 PM.