?
Solved

how can I prevent to construct a array of object

Posted on 2004-11-18
17
Medium Priority
?
238 Views
Last Modified: 2012-05-05
Hi
I am i design my class so any one can create object of that class but he can't create array of object.
Regards
0
Comment
Question by:hansol1
  • 4
  • 2
  • 2
  • +4
13 Comments
 
LVL 10

Accepted Solution

by:
Sys_Prog earned 100 total points
ID: 12617359
Probably
1. disable/privatise [or do not provide a default constructor but provide some other constructor(s)] the no argument or constructor with all arguments defaulted (i.e. default constructor)


Amit
0
 
LVL 10

Expert Comment

by:Sys_Prog
ID: 12617386
But performing the above would NOT let u create an instance with no arguments
Every instance u create would have to be created using a argument

Amit
0
 
LVL 3

Expert Comment

by:esorf
ID: 12617792
Override and make private the array new operator.  This is partial, because you could still make an array on the stack.

- Frank
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 30

Expert Comment

by:Axter
ID: 12617938
>>I am i design my class so any one can create object of that class but he can't create array of object.
Why would you want to do this?
0
 
LVL 14

Expert Comment

by:wayside
ID: 12620962
No matter what, they will always be able to create an array of object pointers. Then with just a few extra lines of code to fill the array and empty it when done, they've accomplished pretty much the same as having an array of objects.

So I don't see the point of what you are trying to do. Can you explain further?  
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12622439
There is always a way to imitate arrays using pointers.

>> 1. disable/privatise [or do not provide a default constructor but provide some other constructor(s)] the no argument or constructor with all arguments defaulted (i.e. default constructor)
how about this?
class1 array[10] = {{ some_arguments }};
0
 
LVL 1

Expert Comment

by:okidachi
ID: 12644512
Hello,

You can create one static integer for your class to count the number of objects (instances) in your class. So when your friends create a new instance, that variable will be increased by 1 and in the constructors you just check this number to be sure that only one instance has been created so far. The code should be like this,

// Code
class MyClass {
public:
      static int n_instances;

      MyClass(void) {
            if (n_instances == 0) { // Okie allow to create an instance
                  n_instances++;
            } else {
                  cout << "You are not allow to create more than one instance" << endl;
                  return;
            }
      }
      ~MyClass(void) { n_instances--; }
};

int MyClass::n_instances = 0;

int main(void)
{
   MyClass MyObj;
   // MyClass YourObj;  -> give the error and exit;
   //  MyClass XObj[5]; -> give the error
   //  MyClass *X = new MyClass[2]; -> give the error

   return 0;
}

The code above works when you create an array of object and even for the pointer to an array of object.
0
 
LVL 3

Expert Comment

by:esorf
ID: 12644725
Of course, okidachi's solution means you can have only one instance *per application*.  As I understand it, the original request would have allowed the class to be used many times, just not in an array.
0
 
LVL 3

Expert Comment

by:esorf
ID: 12644831
Amit's solution is the best and cleanest.  Just require a dummy ctor parameter.  
    MyObject a(0), b(0), c(0);
    MyObject arr[10]; // Error

In many cases (most?) you'll find a useful class doesn't have a meaningful default ctor anyway, so you don't even have to make a dummy parameter.  Just hide the useless default ctor.  It's good practice anyway to hide the default ctor on any new class so you avoid being surprised by an automatically generated default ctor being called when you didn't expect it.

0
 
LVL 9

Assisted Solution

by:jhshukla
jhshukla earned 100 total points
ID: 12651174
counter example:
MyObject arr[10] = {{0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}, {0}};
OR
MyObject arr[10] = {MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0), MyObject(0)};
0
 

Author Comment

by:hansol1
ID: 12651808
Hello Axter
Can you share your class design with me.
0
 
LVL 3

Expert Comment

by:esorf
ID: 12655420
Good point, jhshukla.

So, back to allocation.  We know we can override array new.  But that doesn't handle arrays on the stack.  But if arrays on the stack used array-placement-new, we could override that and catch this case.  Unfortunately, at least in VC6, they don't use an overridable new operator.
0
 

Author Comment

by:hansol1
ID: 12890510
Give m e2-3 days time.I will close the Question
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question