how can I prevent to construct a array of object

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
hansol1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Sys_ProgCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sys_ProgCommented:
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
esorfCommented:
Override and make private the array new operator.  This is partial, because you could still make an array on the stack.

- Frank
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

AxterCommented:
>>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
waysideCommented:
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
jhshuklaCommented:
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
okidachiCommented:
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
esorfCommented:
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
esorfCommented:
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
jhshuklaCommented:
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
hansol1Author Commented:
Hello Axter
Can you share your class design with me.
0
esorfCommented:
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
hansol1Author Commented:
Give m e2-3 days time.I will close the Question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.