Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Calling the constructor

Calling the constructor

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Alexander M
    wrote on last edited by
    #1

    Hi guys, I have a problem concerning a constructor. I have a code like this:

    CClass *pClass;
    pClass = new CClass();
    pClass->CClass::CClass( param1, param2 );

    CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)

    N C S M 4 Replies Last reply
    0
    • A Alexander M

      Hi guys, I have a problem concerning a constructor. I have a code like this:

      CClass *pClass;
      pClass = new CClass();
      pClass->CClass::CClass( param1, param2 );

      CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)

      N Offline
      N Offline
      Nemanja Trifunovic
      wrote on last edited by
      #2

      Simply speaking, you can't. I am actually surprised that it compiles with VC++.


      My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

      A 1 Reply Last reply
      0
      • N Nemanja Trifunovic

        Simply speaking, you can't. I am actually surprised that it compiles with VC++.


        My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

        A Offline
        A Offline
        Alexander M
        wrote on last edited by
        #3

        I have to and I know that it works for GCC, too... I just don't know the syntax, that's the problem. Don't try it, just do it! ;-)

        W 1 Reply Last reply
        0
        • A Alexander M

          I have to and I know that it works for GCC, too... I just don't know the syntax, that's the problem. Don't try it, just do it! ;-)

          W Offline
          W Offline
          Waldermort
          wrote on last edited by
          #4

          You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?

          W A 2 Replies Last reply
          0
          • W Waldermort

            You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?

            W Offline
            W Offline
            Waldermort
            wrote on last edited by
            #5

            CClass *pClass;
            pClass = new CClass();
            pClass->CClass::CClass( param1, param2 );

            What is it you are trying to do here? You are creating a pointer to an object, calling its default constructor which should be

            pClass = new CClass; // no ()

            then trying to call it's overloaded constructor. You can only call one of them, calling the second will only initialise a new object. I'm surprised this compiled.

            T 1 Reply Last reply
            0
            • W Waldermort

              You are not really calling 2 constructors, you are only calling one, then calling an function, which happens to be an overloaded constructor. Overloading functions is ok, but some compilers will complain if you try overloading a constructor. Perhaps you should create just the one and pass null values to the first call. I'm curious to know why you need it to be like this?

              A Offline
              A Offline
              Alexander M
              wrote on last edited by
              #6

              there is no way to re-design it.. i need it exactly as i described!!! i solved the problem using inline assembler now, but it is not the best solution i think. The problem is not the overloaded constructor. the problem is, that the GCC does not want me to call a constructor. Don't try it, just do it! ;-)

              1 Reply Last reply
              0
              • A Alexander M

                Hi guys, I have a problem concerning a constructor. I have a code like this:

                CClass *pClass;
                pClass = new CClass();
                pClass->CClass::CClass( param1, param2 );

                CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)

                C Offline
                C Offline
                cmk
                wrote on last edited by
                #7

                You could try using 'placement new'. e.g. new(pClass) CClass(param1, param2); ...cmk Save the whales - collect the whole set

                1 Reply Last reply
                0
                • A Alexander M

                  Hi guys, I have a problem concerning a constructor. I have a code like this:

                  CClass *pClass;
                  pClass = new CClass();
                  pClass->CClass::CClass( param1, param2 );

                  CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  Why would you want to do such a thing? Steve

                  1 Reply Last reply
                  0
                  • A Alexander M

                    Hi guys, I have a problem concerning a constructor. I have a code like this:

                    CClass *pClass;
                    pClass = new CClass();
                    pClass->CClass::CClass( param1, param2 );

                    CClass has 2 constructors, the first called with new, and the second one has to be called after it. The VC compiler accepts this syntax, but GCC does not. What can I do? Redesign won't work, I need it exactly like this!! Thanks for advice, Alex Don't try it, just do it! ;-)

                    M Offline
                    M Offline
                    Michael Dunn
                    wrote on last edited by
                    #9

                    You can call any ctor with new:

                    pClass = new CClass ( param1, param2 );

                    --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

                    1 Reply Last reply
                    0
                    • W Waldermort

                      CClass *pClass;
                      pClass = new CClass();
                      pClass->CClass::CClass( param1, param2 );

                      What is it you are trying to do here? You are creating a pointer to an object, calling its default constructor which should be

                      pClass = new CClass; // no ()

                      then trying to call it's overloaded constructor. You can only call one of them, calling the second will only initialise a new object. I'm surprised this compiled.

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      waldermort wrote:

                      pClass = new CClass();

                      nop walder, this line is correct. > using no parameter will always implicitely call the default constructor. > using parenthesis without parameters will call the same default constructor, but explicitely. > using some parenthesis with parameters will make the compiler look for the best fitting constructor. you must know that some constructors can be declared as explicit, and such constructors cannot be called without parenthesis !!!!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups