Notices
Results 1 to 3 of 3

Thread: How do i initialize data members?

  1. #1 How do i initialize data members? 
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    I have a node class
    Code:
    class CNode
    {
    string data;
    public:
    CList(string setdata);
    }
    (NOTE: i only want CNode to be able to be initialized if there is a parameter given)

    I have another class that has two CNodes
    Code:
    class CList
    {
    
    CNode node1;
    CNode node2;public:
    //constructors and destructors here..
    
    }
    I can't run the code cause CNode has no default constructors ( meant to be that way ).

    What is the proper syntax to initialize node1 and node2?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  2.  
     

  3. #2  
    Forum Ph.D.
    Join Date
    Jan 2013
    Posts
    908
    I couldn't really find much stuff on google, but after trial and error,

    i got something that worked
    Code:
    class CNode
    {
    string data;
    public:
    CNode(string setdata);
    }
    
    Code:
    class CList{
    CNode node1;
    CNode node2;
    public:
    CList();
    }
    
    CList::CList()
    :node1("asd"),node2("zxc"){
    }
    am i doin it right?


    Quote Originally Posted by jocular View Post
    If thy right nipple offend thee, pluck it off! Goes for the other, too!
    Reply With Quote  
     

  4. #3  
    Forum Radioactive Isotope MagiMaster's Avatar
    Join Date
    Jul 2006
    Posts
    3,440
    Yes, that's the basic problem that syntax is meant to address, so that's the right way to do it.
    Reply With Quote  
     

Similar Threads

  1. Replies: 5
    Last Post: June 19th, 2013, 07:07 PM
  2. Replies: 1
    Last Post: October 3rd, 2012, 12:21 PM
  3. UK members!
    By Emzywb in forum In the News
    Replies: 9
    Last Post: October 12th, 2011, 08:53 AM
  4. New Members
    By Halliday in forum Site Feedback
    Replies: 3
    Last Post: November 4th, 2010, 02:28 AM
  5. Please hlep me in (Data preparation for data mining )
    By nadia in forum Computer Science
    Replies: 3
    Last Post: December 22nd, 2008, 01:56 PM
Bookmarks
Bookmarks
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •