Create a new class object through a function

I want to know if it's possible.

Basically, I want to give the user a choice of making a new object that's part of a class. The user enters the choice and it should call a function "CreateObject()" which makes a new object and stores it in the heap "Class *Object = new Class".

If anyone can lead me to some reference to explain this or explain it themselves it'd be greatly appreciated! I've been looking and I've found nothing so far.

Basically, I want to give the user a choice of making a new object that's part of a class. The user enters the choice and it should call a function "CreateObject()" which makes a new object and stores it in the heap "Class *Object = new Class".

I do not really understand what you are trying to do. What is the bigger picture of what you are trying to do?

At the moment, the answer to your question is trivial:

Code:

if (choice == some_option)
{
Class *Object = new Class;
// ...
}

But that's probably a misinterpretation of what you want.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Is there way to create an object of a class during runtime through a function, and be able to display the critter's age through a function without any scope issues during compile time?

Yes. You need to get your functions to take in arguments and return values, e.g., a pointer to Critter. You can check if the pointer is null to figure out if the user has a critter.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Any chance you could show me through some example code? It'd be greatly appreciated . I'm still new with c++ and even more recently, classes. I know somewhat about using pointers and/or references for functions, but this one stumps me!

Later, you will learn how to use smart pointers so you can make this less error prone, but notice that I added a delete critter; to case 0.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Awesome! Thank you so much! I did notice the delete critter to case 0, that's to free the memory, and setting the pointer to 0 makes it null (I am at least familiar with that). I made an additional condition to case 1 to check if there was already a critter made since (I believe) it wouldn't make sense to re-initialize the whole object. Actually I do have a question on that part. if I were to constantly choose 1 without checking if there was a critter already, would it overwrite the old critter, or just take up more space in the memory?

I made an additional condition to case 1 to check if there was already a critter made since (I believe) it wouldn't make sense to re-initialize the whole object. Actually I do have a question on that part. if I were to constantly choose 1 without checking if there was a critter already, would it overwrite the old critter, or just take up more space in the memory?

Good catch. If you did not check, you would get a memory leak.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

Cool, good to know. I do have another question for the ReleaseCritter function. I haven't seen the use of a pointer and reference used together. I notice it compiles without & and when i run it, it doesn't delete the critter. It doesn't even compile if I just leave the & in there. Shouldn't it just leaving it as a pointer be well enough since it should delete the object seen through the pointer?

I haven't seen the use of a pointer and reference used together. I notice it compiles without & and when i run it, it doesn't delete the critter. It doesn't even compile if I just leave the & in there. Shouldn't it just leaving it as a pointer be well enough since it should delete the object seen through the pointer?

The idea is to pass the pointer by reference so that the pointer will be a null pointer in the caller too. If you pass the pointer by value, the critter is destroyed, but the pointer in the caller does not become a null pointer since the assignment only has an effect on the local pointer parameter.

Originally Posted by Bjarne Stroustrup (2000-10-14)

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.