Thread: A question about class members and constructors

  1. #1
    Registered User Megidolaon's Avatar
    Join Date
    Oct 2008
    Posts
    8

    Question A question about class members and constructors

    I have the following class:
    Code:
    class Stuff
    {
    private:
    	int val;
    
     public:
    	Stuff (int Val)
    	{
    		this.val = Val;
    	}
    
    	int GetVal()
    	{
    		return this.val;
    	}
    	
    	void SetVal(int Val)
    	{
    		this.val = Val;
    	}
    };
    The bold line gives me the following error:
    error C2228: left of '.val' must have class/struct/union

    If I change it to
    Code:
    		Stuff.val = Val;
    I get the following error:
    error C2143: syntax error : missing ';' before '.'

    What would be the correct way of creating a constructor that takes an int and assigns this int to val?
    In general, what would be the correct way to address a variable of the class?
    In C# I'd use this to refer to the current object, but I can't seem get that to work in C++.

    Also, when I create a new Stuff object, what would be the correct syntax?
    Would
    Code:
    A = Stuff;
    create a new, uninitialized object of the Stuff class with static memory
    and would
    Code:
    A = new Stuff(2);
    dynamically allocate a new object the Stuff class, with an initialized value of 2, which is assigned to the variable val?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    this is a pointer, so this.val should be this->val. That said, since there can be no conflict in that context, you might as well just use val (unless you want to use a consistent naming convention so the parameter name would also be val). However, in the constructor, you should use the initialisation list, e.g.,
    Code:
    Stuff(int Val) : val(Val) {}
    Quote Originally Posted by Megidolaon
    Also, when I create a new Stuff object, what would be the correct syntax?
    It depends. You most likely want to write:
    Code:
    Stuff stuff(2);
    I suggest that you read a book like Accelerated C++ for a more comprehensive introduction to C++.
    Last edited by laserlight; 01-28-2009 at 01:15 PM. Reason: Added link.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User Megidolaon's Avatar
    Join Date
    Oct 2008
    Posts
    8
    Thanks, I know made it:
    Code:
    Stuff (int input)
    	{
    		val = input;
    	}
    and the declaration
    Code:
    Stuff x(2);
    Stuff y(3);
    and it worked just fine.

    Though now I have a whole different question.

    I learned that stack memory is precious in C#.
    There you have little control about the memory allocation, objects and arrays will automatically be allocated on the heap.

    However since you have the choice between stack and heap in C++ I wonder where I should put my objects.
    Is it generally best to use mostly pointers and allocate memory for objects on the heap, just like in C#?
    Or do I only have to worry about that when I have arrays/vectors/maps of object or otherwise a large amount of them?
    Last edited by Megidolaon; 01-30-2009 at 02:07 PM.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'd use stack variables whenever possible, since you don't need to worry about memory leaks and because calling new (or malloc() in C) takes a lot longer since the kernel needs to allocate memory for you.
    If you need a large amount of memory or if the variable needs to live longer than to the end of the function, then use new/delete.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    i have a related question; is there any way to do something like this:

    Code:
    template<unsigned size>class c
    {
        int data[size];
        c(const c<size>& _c)
            data(_c.data){}
    };
    or do you just have to loop over size in the ctor body?

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In that case you don't provide a copy constructor and the one that is automatically provided does the right thing.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need the code of few programs in c++.plzzzzz help...plzzz
    By NAVINKR20 in forum C++ Programming
    Replies: 1
    Last Post: 05-08-2009, 09:13 AM
  2. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 09:29 AM
  3. Array members copied by the default copy constructors?
    By cyberfish in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2008, 12:46 AM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM
  5. Replies: 1
    Last Post: 12-11-2002, 10:31 PM