A question about class members and constructors

This is a discussion on A question about class members and constructors within the C++ Programming forums, part of the General Programming Boards category; I have the following class: Code: class Stuff { private: int val; public: Stuff (int Val) { this.val = Val; ...

  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
    21,593
    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 12:15 PM. Reason: Added link.
    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

  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 01: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
    839
    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-17-2008, 11:46 PM
  4. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 03:50 AM
  5. Replies: 1
    Last Post: 12-11-2002, 09:31 PM

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