Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

mattmc

Allowing user to create an instance of class

This topic is 5542 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I''m new to OOP with C++ and wanted to make a simple database type program,in an attempt to get used to OOP. Anyway maybe i''ve missed something very simple but I haven''t come across a method to allow the user to make a new instance of a class, the only way im used to doing so is by writing it into the code. Thanks.

Share this post


Link to post
Share on other sites
Advertisement

  
case USERINTERACTION:
ObjectManager->Add(new CObject);
break;


or maybe


  
rawrr = new CObject;

CObject::CObject()
{
AddToList(this);
}


or maybe I misunderstood the question...

Share this post


Link to post
Share on other sites
Create a dynamic array of the class type you want to let the user create. Then whenever , you enlarge the array by one element, and create the class-instance for that element...

Hope I''m clear.

Some sample-code in Delphi :

// First, declaration of a dynamic TMyClass array, where TMyClass
// is the name the name of the class.
var ArrayWithMyClass = Array of TMyClass;

// Here is a function declaration (in delphi called a procedure here)
// It is triggered when the user clicks a button.
procedure TForm1.Button1Click(Sender: TObject);
begin
// The following line adds one element to our array
SetLength(ArrayWithMyClass, Length(ArrayWithMyClass) + 1);
// And now, we''ve got to create that element. The
// .create is similar to "new" in C++
ArrayWithMyClass[Length(ArrayWithMyClass) - 1] := TMyClass.Create;
end;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!