simple constructor problem

Discussion in 'C++' started by Michael, Mar 6, 2007.

  1. Michael

    Michael Guest

    Hi All,

    if I have a class that has a constant as an attribute like this:

    class SomeClass {
    private:
    const int SOME_CONST;
    int var;
    public:
    SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
    this right?
    void someFunc();
    };

    if I do it like above I can do this:

    SomeClass instance_name();

    but I want to be able to call the constructor with a value for var like
    this:

    SomeClass instance_name(an_integer);

    is it possible to do this without having to include a value for the const in
    the function call?

    Thanks for your help

    Michael
     
    Michael, Mar 6, 2007
    #1
    1. Advertising

  2. * Michael:
    >
    > if I have a class that has a constant as an attribute like this:
    >
    > class SomeClass {
    > private:
    > const int SOME_CONST;


    Java'ism: in C++ reserve all uppercase names for macros.

    Also note that instances of this class won't be assignable.


    > int var;
    > public:
    > SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
    > this right?


    Yes.


    > void someFunc();
    > };
    >
    > if I do it like above I can do this:
    >
    > SomeClass instance_name();
    >
    > but I want to be able to call the constructor with a value for var like
    > this:
    >
    > SomeClass instance_name(an_integer);


    Just provide that constructor.


    > is it possible to do this without having to include a value for the const in
    > the function call?


    ITYM 'constructor call'. Yes. You can either have two constructors
    (one with and one without argument), or one constructor with an argument
    with default value.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Mar 6, 2007
    #2
    1. Advertising

  3. Michael

    Heinz Ozwirk Guest

    "Michael" <> schrieb im Newsbeitrag
    news:45ed4806$0$31058$...
    > Hi All,
    >
    > if I have a class that has a constant as an attribute like this:
    >
    > class SomeClass {
    > private:
    > const int SOME_CONST;
    > int var;
    > public:
    > SomeClass() : SOME_CONST(4),var(0){} // default constructor...is
    > this right?
    > void someFunc();
    > };
    >
    > if I do it like above I can do this:
    >
    > SomeClass instance_name();


    Additionally to what Alf already wrote, you cannot do that. That declares a
    function with no arguments, which is supposed to return an instance of
    SomeClass. To define a variable of type SomeClass, simply write

    SomeClass instance_name;

    HTH
    Heinz
     
    Heinz Ozwirk, Mar 6, 2007
    #3
  4. Michael

    Michael Guest

    "Alf P. Steinbach" <> wrote in message
    news:...
    >* Michael:
    >>
    >> if I have a class that has a constant as an attribute like this:
    >>
    >> class SomeClass {
    >> private:
    >> const int SOME_CONST;

    >
    > Java'ism: in C++ reserve all uppercase names for macros.
    >
    > Also note that instances of this class won't be assignable.
    >
    >
    >> int var;
    >> public:
    >> SomeClass() : SOME_CONST(4),var(0){} // default
    >> constructor...is this right?

    >
    > Yes.
    >
    >
    >> void someFunc();
    >> };
    >>
    >> if I do it like above I can do this:
    >>
    >> SomeClass instance_name();
    >>
    >> but I want to be able to call the constructor with a value for var like
    >> this:
    >>
    >> SomeClass instance_name(an_integer);

    >
    > Just provide that constructor.


    for some reason I couldn't see how to do this yesterday....hence my
    questioning if it was possible.....today it is obvious....

    Thanks for the help :)

    >
    >
    >> is it possible to do this without having to include a value for the const
    >> in the function call?

    >
    > ITYM 'constructor call'. Yes. You can either have two constructors (one
    > with and one without argument), or one constructor with an argument with
    > default value.
    >
    > --
    > A: Because it messes up the order in which people normally read text.
    > Q: Why is it such a bad thing?
    > A: Top-posting.
    > Q: What is the most annoying thing on usenet and in e-mail?
     
    Michael, Mar 6, 2007
    #4
  5. Michael

    Michael Guest

    Ok, so here's another question:

    is it better to declare the constructor body within the class declaration
    like:

    class SomeClass {
    private:
    const int SOME_CONST;
    int var;
    public:
    SomeClass() : SOME_CONST(4),var(0){}
    SomeClass(int i) : SOME_CONST(4),var(i){}
    int getVar();
    };

    or just declare the prototype and then do the body later like:

    class SomeClass {
    private:
    const int SOME_CONST;
    int var;
    public:
    SomeClass();
    SomeClass(int i);
    int getVar();
    };

    SomeClass::SomeClass() : SOME_CONST(4),var(0){}
    SomeClass::SomeClass(int i) : SOME_CONST(4),var(i){}

    and why?

    Thanks for your help

    Regards

    Michael
     
    Michael, Mar 7, 2007
    #5
  6. Michael

    Kai-Uwe Bux Guest

    Michael wrote:

    > Ok, so here's another question:
    >
    > is it better to declare the constructor body within the class declaration
    > like:
    >
    > class SomeClass {
    > private:
    > const int SOME_CONST;
    > int var;
    > public:
    > SomeClass() : SOME_CONST(4),var(0){}
    > SomeClass(int i) : SOME_CONST(4),var(i){}
    > int getVar();
    > };
    >
    > or just declare the prototype and then do the body later like:
    >
    > class SomeClass {
    > private:
    > const int SOME_CONST;
    > int var;
    > public:
    > SomeClass();
    > SomeClass(int i);
    > int getVar();
    > };
    >
    > SomeClass::SomeClass() : SOME_CONST(4),var(0){}
    > SomeClass::SomeClass(int i) : SOME_CONST(4),var(i){}
    >
    > and why?


    The technical difference between the two is that in version 1, the
    constructor is declared inline whereas in version 2, you would need to use
    the keyword inline to achieve that. Other than that, it all depends on how
    you like to organize your code in header and implementaion files.

    Personally, I put everthing into what would commonly be called header files
    (my code base is heavily templated, and my compiler does not support
    export). Therefore, I go with the first option. It is less verbose. Some
    people still like to separate header and implementation files (which would
    be included by the header file in case of templated). I think, this is
    mistaken as there seems to be an underlying confusion: namely that header
    files are some sort of documentation.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Mar 7, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giulio
    Replies:
    9
    Views:
    1,064
    Patrick Kowalzick
    Jun 25, 2003
  2. Brett Irving
    Replies:
    3
    Views:
    3,346
    John Harrison
    Jun 29, 2003
  3. lallous
    Replies:
    5
    Views:
    8,858
    David Harmon
    Jan 23, 2004
  4. Aire
    Replies:
    3
    Views:
    484
    Mike Wahler
    Jan 25, 2004
  5. Generic Usenet Account
    Replies:
    10
    Views:
    2,323
Loading...

Share This Page