-
May 28th, 2010, 12:42 AM
#1
[VC++] Creating objects on the fly
I have been searching for quite a while to no avail for this answer, and was hoping that someone may have the answer here.
I'm not new to c++; I have been working with it for about 3 years now, and recently moved into visual c++ (with Microsoft Visual Studio 2008), where I quickly ran into a problem I have yet to answer.
My question seems easy enough; is it possible to create objects (such as labels or picture boxes) on the fly from within functions, rather than in the namespace constructor?
I know the general creation code defined with the handles/pointers in the beginning of the form's namespace, but I'm wondering if - through a function - it is possible to create and destroy them?
And if so, what is the proper syntax, and how would I be able to - if at all possible - port it over to an external .cpp file?
Considering the diversity of the C++ language, I believe that the answer to my problems exist, but I have yet to find out precisely what it is.
Any potential help would be appreciated.
-
May 28th, 2010, 12:57 AM
#2
Re: [VC++] Creating objects on the fly
I know the general creation code defined with the handles/pointers in the beginning of the form's namespace
I have no idea what you mean by this, but you can simply create a object runtime whenever and wherever you want.
-
May 28th, 2010, 07:30 AM
#3
Re: [VC++] Creating objects on the fly
I think maybe the OP is using managed C++. There's a different forum for that.
-
May 28th, 2010, 07:35 AM
#4
Re: [VC++] Creating objects on the fly
Originally Posted by Skizmo
I have no idea what you mean by this, but you can simply create a object runtime whenever and wherever you want.
Pretty much nothing in that post made sense to me, but I agree that he's probably talking about managed C++.
-
May 31st, 2010, 08:31 PM
#5
Re: [VC++] Creating objects on the fly
Sorry, I thought I subscribed to this when I created this question.
What exactly do you mean by "managed c++"? I'm not using the MFC, if that's what you're referring to, just the normal visual form creation built into Visual Studio 2008.
I was referring to the initialization of components and declaration of the components such as
Code:
private: System::Windows::Forms::Timer^ tmr_Refresh;
private: System::Windows::Forms::PictureBox^ Player;
private: System::Windows::Forms::ImageList^ imgSpritelist;
private: System::Windows::Forms::Label^ lblText;
(the old code, which I believe actually used the * pointers to declare)
and
Code:
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Game::typeid));
What I was saying is that i know the code for the initialization of components from within the form itself, but I don't know if there is a way to create said objects from inside a function from a different cpp/header file.
-
May 31st, 2010, 11:02 PM
#6
Re: [VC++] Creating objects on the fly
Originally Posted by TheDiabolicalPupeteer
What exactly do you mean by "managed c++"?
They mean "managed C++": http://en.wikipedia.org/wiki/Managed...ns_for_C%2B%2B
-
June 1st, 2010, 01:38 AM
#7
Re: [VC++] Creating objects on the fly
1. Yes, you can create objects on the fly, using the same syntax as in Wizard-generated code. Take a look at any control produced by Wizard: you need control variable to keep reference, the control must be created and added to the form.
2. By default, C++/CLI Wizard placed all code to h-file. This is probably done because the same Wizard works also for C# and VB .NET, which don't have h and .cpp files. For every Windows form, there is also .cpp file which contains only
#include "Form1.h"
line. You can move implementation to this .cpp file using general C++ syntax.
-
June 1st, 2010, 08:06 PM
#8
Re: [VC++] Creating objects on the fly
Originally Posted by Alex F
1. Yes, you can create objects on the fly, using the same syntax as in Wizard-generated code. Take a look at any control produced by Wizard: you need control variable to keep reference, the control must be created and added to the form.
2. By default, C++/CLI Wizard placed all code to h-file. This is probably done because the same Wizard works also for C# and VB .NET, which don't have h and .cpp files. For every Windows form, there is also .cpp file which contains only
#include "Form1.h"
line. You can move implementation to this .cpp file using general C++ syntax.
Ah thank you, that's the information I was looking for.
I don't suppose you may be able to just give me a quick example piece of code that I could use to base my own creation off of?
I apologize if I'm asking for so much; as I said earlier, most of my work was done with console.
-
June 2nd, 2010, 12:57 AM
#9
Re: [VC++] Creating objects on the fly
Create C++/CLI Windows Forms application. Add button to the form, let's say, button1. Make search in the project for button1. You will see that there is button1 class member declared in the class. In the function which is called InitializeComponent, this button is created, required properties are assigned to it, and the button is added to the form.
In your own code, you need to do exactly the same, in any place where you want.
-
June 7th, 2010, 09:29 PM
#10
Re: [VC++] Creating objects on the fly
Originally Posted by Alex F
Create C++/CLI Windows Forms application. Add button to the form, let's say, button1. Make search in the project for button1. You will see that there is button1 class member declared in the class. In the function which is called InitializeComponent, this button is created, required properties are assigned to it, and the button is added to the form.
In your own code, you need to do exactly the same, in any place where you want.
Sorry for the delayed response.
I get what you're saying about how to do this, but the reason I ask for an actual example of how to do this is because the form, using this managed code, is created with a namespace (followed by inclusion of other namespaces) and then the function that creates the files, correct?
Before that there is also the declaration (either public or private) of said variable.
If this were ported over to another .cpp file, I wouldn't be able to redefine the same namespace - that would cause a name error. Do I just create
Code:
void InitializeComponent(void)
{ //...
}
as a separate function?
There's just a lot of differing code there that gets me confused as to what is needed to be isolated and ported over to achieve what I am asking for.
edit:
on that note, would I have to create the function (with all the code) in only the header file? Or would I create the prototype in the header, and then define the code in the corresponding .cpp file?
Also, is there a way to create an array of an object type?
I remember Visual Basic used to have a way to design arrays of, say, images. Is there a way to define something like a label in the same method?
Last edited by TheDiabolicalPupeteer; June 8th, 2010 at 01:18 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|