CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2010
    Posts
    10

    constructor being ignored in visual studio 2010

    I'm not used to using VS and when I realized my constructor was being ignored, you can understand why I would be confused

    example:

    VTXMENU::VTXMENU()
    {
    Submenu = NULL;
    Next = NULL;
    Previous = NULL;
    Parent = NULL;
    Base = NULL;
    ItemType = 0;
    ItemName[0] = 0; // (these two are character-array strings)
    Command[0] = 0; //
    cout<< "created\n";
    }


    and I'm creating one dynamically via:
    VTXMENU *fish;
    fish = new VTXMENU;


    not only do my strings end up full of garbage, but the message "created" is never displayed

    Why is this happening?
    thx

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: constructor being ignored in visual studio 2010

    Quote Originally Posted by invaderjolleyolleyman View Post
    and I'm creating one dynamically via:
    VTXMENU *fish;
    fish = new VTXMENU;


    not only do my strings end up full of garbage, but the message "created" is never displayed

    Why is this happening?
    thx
    Is VTXMENU a struct or a class? I'm not sure if I correctly interpreted the wording, but it seems like VC++ treats structs and classes differently when you create an object with new as you do.
    http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx
    Quote Originally Posted by MSDN
    If an object is of a class type and that class has constructors (as in the preceding example), the object can be initialized by the new operator only if one of these conditions is met:
    - The arguments provided in the initializer agree with those of a constructor.
    - The class has a default constructor (a constructor that can be called with no arguments).
    It seems like "class type" does not include a struct and, therefore, your object is not initialized when you call new like this. To make sure the default constructor is called use this instead:
    Code:
    VTXMENU *fish = new VTXMENU();
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: constructor being ignored in visual studio 2010

    Hmmm.....that seems unlikely. There should be no appreciable difference between structs and classes on that level.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: constructor being ignored in visual studio 2010

    Code:
    #include <iostream>
    
    using namespace std;
    
    struct foo
    {
        int a;
    
        foo()
        {
            std::cout << "hello\n";
        }
    };
    
    int main()
    {
        foo* pFoo1 = new foo;
        foo* pFoo2 = new foo();
    }
    Objects are properly constructed for both C::B+MinGW, as well as VS2008.

    I think a short but complete code example could shed some light on this.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: constructor being ignored in visual studio 2010

    Quote Originally Posted by invaderjolleyolleyman View Post
    I'm not used to using VS and when I realized my constructor was being ignored, you can understand why I would be confused
    If a compiler such as Visual C++ 2010 can't call a simple constructor, then thousands of programmers would have reported the problem.

    So post a complete example, as the chance of constructors being ignored in one of the most used C++ compilers in the world is practically zero.

    Regards,

    Paul McKenzie

  6. #6
    Join Date
    Jul 2010
    Posts
    10

    Re: constructor being ignored in visual studio 2010

    Quote Originally Posted by D_Drmmr View Post
    Is VTXMENU a struct or a class? I'm not sure if I correctly interpreted the wording, but it seems like VC++ treats structs and classes differently when you create an object with new as you do.
    http://msdn.microsoft.com/en-us/libr...(v=VS.80).aspx

    It seems like "class type" does not include a struct and, therefore, your object is not initialized when you call new like this. To make sure the default constructor is called use this instead:
    Code:
    VTXMENU *fish = new VTXMENU();
    I'm using a class (not a struct)
    I thought I mentioned earlier that putting the parenthesis didn't help

    I know this doesn't make sense or I wouldn't be asking about it ^_^


    if a thorough example is what you guys want, then you shall receive it...


    from main.cpp :
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    int main()
    {
    VTXMENU* root;


    root = new VTXMENU;
    cout<< root->Command << endl;
    delete root;

    root = new VTXMENU();
    cout<< root->Command << endl;
    delete root;


    system("pause");
    return 0;
    }
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



    from VTXMENU.h :
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    class VTXMENU
    {
    public:
    VTXMENU* Submenu;
    VTXMENU* Next;
    VTXMENU* Previous;
    VTXMENU* Parent;
    VTXMENU* Base;
    char ItemName[40];
    char Command[40];
    int ItemType;

    VTXMENU();
    VTXMENU(VTXMENU &old);
    ~VTXMENU();
    VTXMENU operator = (VTXMENU &old);

    void Release();

    };
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



    constructor from VTXMENU.CPP :
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    VTXMENU::VTXMENU()
    {
    Submenu = NULL;
    Next = NULL;
    Previous = NULL;
    Parent = NULL;
    Base = NULL;
    ItemType = 0;
    ItemName[0] = 0;
    Command[0] = 0;
    cout<< "initialized\n";
    }
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////





    both time that the contents of Command are displayed, it shows something similar to:
    =========================

    also, the word 'initialized' is never displayed


    none of this seems rational at all
    maybe my computer has a hole in its head or something


    for some reason I've been having trouble getting this project to build well
    I had this same problem a few days ago but it seemed to go away on it's own when I added an arbitrary include to one file, built, removed the include, then built again...
    ( I tried that again this morning but it didn't help XD )

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: constructor being ignored in visual studio 2010

    Quote Originally Posted by invaderjolleyolleyman View Post
    both time that the contents of Command are displayed, it shows something similar to:
    =========================

    also, the word 'initialized' is never displayed


    none of this seems rational at all
    maybe my computer has a hole in its head or something


    for some reason I've been having trouble getting this project to build well
    I had this same problem a few days ago but it seemed to go away on it's own when I added an arbitrary include to one file, built, removed the include, then built again...
    ( I tried that again this morning but it didn't help XD )
    This code, pasted into my test project (after I fixed a "missing destructor" error) works OK: I see two 'initialized'.
    Could you try that? Does it work for you?
    Something else must be wrong in your project.
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Jul 2010
    Posts
    10

    Re: constructor being ignored in visual studio 2010

    wow
    I got it to work but it's tempting just to let you guys fight over it for a while before telling you what happened

    Apparently, when using visual studio 2010, it is not only good practice but in fact required to say funcName(void) rather than just funcName() when declaring your functions
    I'm not exactly upset about this but I can't help but wonder why no one in any of the books I have read or the college courses I have taken have ever mentioned this

    I've only recently been inserting bits like that just for style purposes


    I wonder it will stop working again tomorrow without me changing anything - lol

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: constructor being ignored in visual studio 2010

    That isn't required.

    I'll bet what happened, though, was that your file's modification date got screwed up somehow, so that the version containing the constructor wasn't being recompiled. When you added the void, you forced it to be recompiled. That's my guess anyway.

  10. #10
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: constructor being ignored in visual studio 2010

    Code:
    ItemName[0] = 0; // (these two are character-array strings)
    Command[0] = 0; //
    If these are character arrays of length X, then you are only initializing the first element. Therefore the elements (1 - (X-1)) won't be initialized. It doesn't matter that much but if you are looking at the memory in debug the uninitialized memory could look like what you might call "garbage".

    Moreover you didn't flush the output stream so there is no guarantee that the message will be printed when the constructor executes. Use std::endl if you want to add that guarantee otherwise depending on the compiler you may or may not see it at all.

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
  •  





Click Here to Expand Forum to Full Width

Featured