[VC++] Creating objects on the fly
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: [VC++] Creating objects on the fly

  1. #1
    Join Date
    May 2010
    Posts
    7

    [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.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

  3. #3
    Join Date
    Feb 2005
    Posts
    2,160

    Re: [VC++] Creating objects on the fly

    I think maybe the OP is using managed C++. There's a different forum for that.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,088

    Re: [VC++] Creating objects on the fly

    Quote Originally Posted by Skizmo View Post
    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++.

  5. #5
    Join Date
    May 2010
    Posts
    7

    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.

  6. #6
    Join Date
    Dec 2008
    Posts
    144

    Re: [VC++] Creating objects on the fly

    Quote Originally Posted by TheDiabolicalPupeteer View Post
    What exactly do you mean by "managed c++"?
    They mean "managed C++": http://en.wikipedia.org/wiki/Managed...ns_for_C%2B%2B

  7. #7
    Join Date
    Jul 2002
    Posts
    2,512

    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.

  8. #8
    Join Date
    May 2010
    Posts
    7

    Re: [VC++] Creating objects on the fly

    Quote Originally Posted by Alex F View Post
    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.

  9. #9
    Join Date
    Jul 2002
    Posts
    2,512

    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.

  10. #10
    Join Date
    May 2010
    Posts
    7

    Re: [VC++] Creating objects on the fly

    Quote Originally Posted by Alex F View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center