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
Main Topics
Browse All TopicsHi
I am i design my class so any one can create object of that class but he can't create array of object.
Regards
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
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?
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 }};
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.
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.
Business Accounts
Answer for Membership
by: Sys_ProgPosted on 2004-11-18 at 10:30:36ID: 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