Namespace and Hidden This ponter?

Hi there!

I am very new at c++ programming and i must admit that i never take up with any kind of programming language, that means that i am a completely noob at programming!

My first question is does using namespace std; should be placed right after when we actually need it or should be places after the header file declaration?

My second question is i am trying to learn about *this hidden pointer and it is very confusing to me, so could you explain more clear as possible, because in addition to http://www.learncpp.com/ i find tutorial on youtube too about *this pointer and even so is somewhat confusing to me, what do you thing? Here is the video http://www.youtube.com/watch?v=o2t5CO2KTug


For my first question i am sure that the right coding, is to place it when we need it, that is at least what i am learned with tutorials on this site.

P.S. Excuse me for my English, i am not from BG.
Last edited on
Hi CpluplusNoob1,

the using namespace std; declaration is conventionally placed after your list of header files just before main(), but you can place it at any scope and then it will only be valid at that scope, and hence any classes you use within that scope will be searched for within the std namespace.

Regarding the this keyword, in simple terms it allows you to access members (via a pointer, called 'this') of an object belonging to the class, whilst within the scope of that class. The reason you need 'this' is because the class is yet to be instantiated and hence there is no object to refer to if you need to access its members to implement part of the class.

Also, see http://www.cplusplus.com/doc/tutorial/classes2/

good luck!
Thank you for your answer! ;-) Last question - in posted video about 2:54 length, it show the *this pointer

All i got is that *this pointer after it's declaration this->x = dArg;

now refer to the member variable double x; outside of local scope, am i right?;-)

So if you dont wanna see the video, here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13

class CMyClass
	{
	public:
		CMyClass() : x(2.0) {}
		void Set(double dArg)
		{
			double x(3.0) ;
			this->x = dArg;
		}

		double x;
	};



http://img845.imageshack.us/i/captureea.jpg

(CDummy& param) In this case CDummy, or param is reference? I thing the reference is param.;-)
Last edited on
there's no real need in the this keyword in that function, it could just as well be x =dArg;

An example of when you might need to use the this pointer is when an object wants to pass itself into a function from inside of one of it's own member functions, the object would dereference the this pointer *this to pass in the object itself
@quirkyusername

Right in this example that i post, you are wrong, because the results are completely different with using this and without. See the video.;-)

Hi CplusplusNoob1,

in your example, I think you are right that the answer will depend on whether you use the this keyword or not.

The reason is, is that without the this keyword, without a full qualification of the double x that is local to CMyClass::Set the default scope will be the local scope and hence x= dArg. However, since this x and dArg are local to CMyClass::Set then they wont exist after exiting the function and CMyClass::x remains equal to its member list initialised value of 2.0.

However, with the this keyword you are referring to CMyClass::x rather than the x local to CMyClass::Set and hence CMyClass::x = dArg after calling CMyClass::Set, rather than 2.0.

So yes, if you declare variables local to member functions within a class which has data members of the same name then the this keyword will matter. However, you probably shouldn't do this and if you don't then you wouldn't need the this keyword in this case.
Topic archived. No new replies allowed.