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. Basic OOP question...

Basic OOP question...

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 5 Posters 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio

    P S U 3 Replies Last reply
    0
    • L Lost User

      Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio

      P Offline
      P Offline
      Paul Selormey
      wrote on last edited by
      #2

      Hello, You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example, class Basic { Basic(int nOne); .... } Here you cannot say... Basic* pBasic = new Basic; // since nOne is not known Basic theBasic; You will have to say Basic* pBasic = new Basic(1111); // for instance Basic theBasic(1111); For the definition class Basic { Basic(); ...... } You can write... Basic theBasic; Basic* pBasic = new Basic; OR Basic* pBasic = new Basic(); I hope this clears the problem. If it does, then consider a little more study of constructors/destructors. Bye, Paul. ================== The original message was: Hi,

      I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

      Thanks, Bye !
      Braulio

      L 1 Reply Last reply
      0
      • P Paul Selormey

        Hello, You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example, class Basic { Basic(int nOne); .... } Here you cannot say... Basic* pBasic = new Basic; // since nOne is not known Basic theBasic; You will have to say Basic* pBasic = new Basic(1111); // for instance Basic theBasic(1111); For the definition class Basic { Basic(); ...... } You can write... Basic theBasic; Basic* pBasic = new Basic; OR Basic* pBasic = new Basic(); I hope this clears the problem. If it does, then consider a little more study of constructors/destructors. Bye, Paul. ================== The original message was: Hi,

        I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

        Thanks, Bye !
        Braulio

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Thanks but..., Sorry, the problems that I have are with the following derived class: class CDrawToScreenThread : public CGeneralDrawingThread { CDrawToScreenThread (){} // I f not, give a compiler error CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params... } } ================== The original message was: Hello,

        You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,

        class Basic
        {
        Basic(int nOne);
        ....
        }

        Here you cannot say...
        Basic* pBasic = new Basic; // since nOne is not known
        Basic theBasic;

        You will have to say
        Basic* pBasic = new Basic(1111); // for instance
        Basic theBasic(1111);

        For the definition
        class Basic
        {
        Basic();
        ......
        }

        You can write...
        Basic theBasic;
        Basic* pBasic = new Basic;
        OR Basic* pBasic = new Basic();

        I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.

        Bye,
        Paul.

        ==================
        The original message was:

        Hi,

        I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

        Thanks, Bye !
        Braulio

        H 1 Reply Last reply
        0
        • L Lost User

          Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio

          S Offline
          S Offline
          sandrine
          wrote on last edited by
          #4

          ================== The original message was: Hi,

          I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

          Thanks, Bye !
          Braulio Hi Braulio! example: Class A { public: A(int x, int y) {...} ... } int main() { A p(1,2); // Call the constructor defined A p; // incorrect! the default constructor doesn't exist. You have to declare it. ... } If you defined your own constructor, you have to respect the list of arguments. Not if you add the default constructor (without argument). Is your case? Tchao Sandrine

          L 1 Reply Last reply
          0
          • S sandrine

            ================== The original message was: Hi,

            I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

            Thanks, Bye !
            Braulio Hi Braulio! example: Class A { public: A(int x, int y) {...} ... } int main() { A p(1,2); // Call the constructor defined A p; // incorrect! the default constructor doesn't exist. You have to declare it. ... } If you defined your own constructor, you have to respect the list of arguments. Not if you add the default constructor (without argument). Is your case? Tchao Sandrine

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Hi Sandrine ! Good To hear from you here in CodeProject ! I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy). Ciao ! Braulio

            L 1 Reply Last reply
            0
            • L Lost User

              Hi Sandrine ! Good To hear from you here in CodeProject ! I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy). Ciao ! Braulio

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              The VC++ docs say that a default constructor is generated if (and only if) no other constructor has been defined. Once you define the constructor with parameters, you must also explicitly define the 'default'constructor if it is needed. ================== The original message was: Hi Sandrine !

              Good To hear from you here in CodeProject !

              I'm not sure what have I made wrong, It was with a class that inherited from a CWinThread, and this class I declare it as a dinamyc class ( I think that is the name :-( ), then I think the problem is that it needs a default constructor, but I'm not quite sure... ( sometimes C++ is a "pretty" maze and the easiest thing make me a little bit crazy).

              Ciao !
              Braulio

              1 Reply Last reply
              0
              • L Lost User

                Thanks but..., Sorry, the problems that I have are with the following derived class: class CDrawToScreenThread : public CGeneralDrawingThread { CDrawToScreenThread (){} // I f not, give a compiler error CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params... } } ================== The original message was: Hello,

                You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,

                class Basic
                {
                Basic(int nOne);
                ....
                }

                Here you cannot say...
                Basic* pBasic = new Basic; // since nOne is not known
                Basic theBasic;

                You will have to say
                Basic* pBasic = new Basic(1111); // for instance
                Basic theBasic(1111);

                For the definition
                class Basic
                {
                Basic();
                ......
                }

                You can write...
                Basic theBasic;
                Basic* pBasic = new Basic;
                OR Basic* pBasic = new Basic();

                I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.

                Bye,
                Paul.

                ==================
                The original message was:

                Hi,

                I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

                Thanks, Bye !
                Braulio

                H Offline
                H Offline
                Henk Devos
                wrote on last edited by
                #7

                I suppose you get the compiler error in a IMPLEMENT_DYNCREATE macro. When you want to be able to create the thread with AfxBeginThread, your thread gets instantiated for you and therefore MFC will call the constructor. The logic for this is in CObject//CreateObject i believe. The MFC logic has no way to pass parameters to your c onstructor, so whenenver you use a DECLARE_DYNCREATE macro you need a default constructor. ================== The original message was: Thanks but...,

                Sorry, the problems that I have are with the following derived class:

                class CDrawToScreenThread : public CGeneralDrawingThread
                {

                CDrawToScreenThread (){} // I f not, give a compiler error
                CDrawToScreenThread (CWnd *pWnd, ScreenParam ScreenParams); // My constructor with params...
                }
                }

                ==================
                The original message was:

                Hello,

                You should have posted some codes. From the written it seems you are using a constructor with parameters as if it is one without a parameter. For the one with the parameter, when creating an instance of the object you need to supply the parameters. For example,

                class Basic
                {
                Basic(int nOne);
                ....
                }

                Here you cannot say...
                Basic* pBasic = new Basic; // since nOne is not known
                Basic theBasic;

                You will have to say
                Basic* pBasic = new Basic(1111); // for instance
                Basic theBasic(1111);

                For the definition
                class Basic
                {
                Basic();
                ......
                }

                You can write...
                Basic theBasic;
                Basic* pBasic = new Basic;
                OR Basic* pBasic = new Basic();

                I hope this clears the problem. If it does, then consider a little more study of constructors/destructors.

                Bye,
                Paul.

                ==================
                The original message was:

                Hi,

                I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ?

                Thanks, Bye !
                Braulio

                1 Reply Last reply
                0
                • L Lost User

                  Hi, I have a basic OOP question, I have a class ( a base class, and don't inherits from CObject), and I want to have a constructor with some parameters, I make this, but when I want to compile it without errors I have to add an empty constructor without parameters. Why must I do this ? Have I made something wrong ? Thanks, Bye ! Braulio

                  U Offline
                  U Offline
                  User 1472
                  wrote on last edited by
                  #8

                  This is a basic C++ question, not OOP. When ever you create a class the compiler automatically creates a default constructor and a copy constructor. The default constructor is required if you want to create objects like this:CFoo foo;
                  As soon as you create ANY constructor, the compiler requires you to write ALL constructors. In other words, it no longer creates the default constructor for you. That means if you write:struct Point { int x; int y; }; Point origin; // legal - using compiler-supplied default constructor
                  ...but if you write this...struct Point { int x; int y; Point(int x, int y): x(x), y(y) {} };
                  ...the compiler no longer generates the default constructor...Point origin; // illegal! no default constructor Point origin (12,45); // legal - using Point(int,int)
                  Note that a default constructor does not have to take 0 arguments, you simply must be able to call it with 0 arguments. Like this...struct Point { int x; int y; Point(int x=0, int y=0): x(x), y(y) {} // default parameters };
                  Now everything's happy again...Point origin; // legal - using Point(int,int) Point origin (12,45); // legal - using Point(int,int)
                  Hope this clarifies more than it confuses! Cheers, Eric

                  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