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

    How to initialize null vector?

    Hi all,

    I'm trying to initialize a vector as null in a class constructor but it obviously gives me an error because the "null" is an int but not a vector<>.

    Is there any way that I can initialize the member "vector<P> choices" as a null so I can do something like the snippet below for validation of values:

    Code:
    if(this->choices != null)
    {
        // check if value is in the vector
    }
    else
    {
        // regular validation
    }

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

    Re: How to initialize null vector?

    Are you coming from a Java background? NULL can only be applied to pointers. You could make the class member a vector pointer, but that's not a good solution if you don't have to; pointer members are always harder to deal with!

    A vector is default-constructed to be empty. You can just do your check as
    Code:
    if (!this->choices.empty())

  3. #3
    Join Date
    Sep 2007
    Posts
    15

    Re: How to initialize null vector?

    I'm actually coming from everything but Java. I've got more experience from dynamically typed languages (Perl, PHP, Python) but I get your point.

    I'm trying to avoid the overloaded constructors by placing all the attributes in the same constructor but with a default = null. Is there any standard way to do that in C++?
    Last edited by Hannson; December 8th, 2008 at 07:02 PM.

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

    Re: How to initialize null vector?

    Quote Originally Posted by Hannson View Post
    I'm trying to avoid the overloaded constructors by placing all the attributes in the same constructor but with a default = null. Is there any standard way to do that in C++?
    There is no such thing as a "null" vector. It is either empty or it isn't empty.

    Regards,

    Paul McKenzie

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

    Re: How to initialize null vector?

    Quote Originally Posted by Hannson View Post
    I'm trying to avoid the overloaded constructors by placing all the attributes in the same constructor but with a default = null. Is there any standard way to do that in C++?
    Well, that doesn't work 100&#37; of the time; there are a few cases where an actual, no-arguments default constructor is required.

    In general, though, you should be assigning non-pointer members to their default-constructed value. That's 0 for built-in types including pointers, and ClassName() for class types.

    If you want to avoid writing more constructors then you have to, then *definitely* don't use pointer members----usually when you do that you're stuck either writing a copy constructor and operator=, or disabling those by declaring them private so the class is uncopyable.
    Last edited by Lindley; December 8th, 2008 at 07:30 PM.

  6. #6
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to initialize null vector?

    Quote Originally Posted by Hannson View Post
    I'm trying to avoid the overloaded constructors by placing all the attributes in the same constructor but with a default = null. Is there any standard way to do that in C++?
    Typically that is a BAD idea. You are much better off being as expressive as possible when developing classes, it greatly reduces the risk of bugs when you go to use the class.

    Remember that you will write a given class 1 time. You may use it 1000 times. The investement in implementing the class 100% properly is well worth it.

    REmember to ALWAYS strive for code that is both:

    1) Easy to USE (not necessarily easy to WRTIE) correct.
    2) HARD to Use incorrectly.

    Default parameters can increase the possibility of using the class incorrectly.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  7. #7
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    Re: How to initialize null vector?

    Others have said it before me, but I'll add my voice to the throng...

    Quote Originally Posted by Hannson View Post
    I'm trying to avoid the overloaded constructors [...]
    Why?
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
    --
    Sutter and Alexandrescu, C++ Coding Standards

    Programs must be written for people to read, and only incidentally for machines to execute.

    --
    Harold Abelson and Gerald Jay Sussman

    The cheapest, fastest and most reliable components of a computer system are those that aren't there.
    -- Gordon Bell


  8. #8
    Join Date
    Sep 2007
    Posts
    15

    Re: How to initialize null vector?

    OK I see your points.

    Why?
    I'm more used to weak typing I guess...

  9. #9
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: How to initialize null vector?

    Scott Meyers said it best back in the 1990's...

    Paraphrased:
    Code:
    If you don't like doing things the correct way in C++, go program in Fortran or some other language.
    Attempting to "coerce" any language into pre-concieved concepts is always a mistake. This is why, no matter how many languages you alread know, it is important to "wipe your mind" and learn the new language in a structured fashion (ie. by carefully following a good book).

    Even with over 30 years experience, I still follow this process when learning a new language. Often the difference between something "worning" and "being correct" is only covered in one sentance which can easily be missed be other means.

    Once one things they know (a part of) the language, attempting to read an understand the formal language specification is a good idea. If something does not make sense (in that section) then it is probably time to go back and repeat the book from that point on.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  10. #10
    Join Date
    Sep 2007
    Posts
    15

    Re: How to initialize null vector?

    Interesting,

    Then I guess I have to rethink my project. Thanks for your help!

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