simplifying constructors in struct

This is a discussion on simplifying constructors in struct within the C++ Programming forums, part of the General Programming Boards category; Is there a way to combine these constructors into one? Code: struct Node { Node() : next(NULL) { } Node(string ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116

    simplifying constructors in struct

    Is there a way to combine these constructors into one?
    Code:
    struct Node
            {
                Node() : next(NULL)
                {
    
                }
    
                Node(string data) : next(NULL) : this->data(data)
                {
    
                }
    
                string data;
                Node *next;
            };

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I don't think so. Is there a reason why you would want to?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    Well, you could write:
    Code:
    struct Node
    {
        Node(const std::string& data_ = std::string()) : data(data_), next(NULL) {}
    
        std::string data;
        Node* next;
    };
    You might want to consider declaring the constructor as explicit.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    Quote Originally Posted by laserlight View Post
    Well, you could write:
    Code:
    struct Node
    {
        Node(const std::string& data_ = std::string()) : data(data_), next(NULL) {}
    
        std::string data;
        Node* next;
    };
    You might want to consider declaring the constructor as explicit.
    That's what I want. I've never seen this before
    Code:
    void myFunc(int i = 1)
    {
    //etc.
    }
    what is it called when you assign a value in the parameters and how does it work?

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    It's a default argument, when you supply fewer arguments to the function than it expects, the default value is used to initialize the argument.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Consider replacing NULL with nullptr as well, if your compiler supports it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    116
    If the struct is in the header file, does this mean all this is inline?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Yes, but you can separate the implementation from the definition just as with classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  3. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  4. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  5. Replies: 10
    Last Post: 05-18-2006, 11:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21