tags:

views:

94

answers:

4

Hi, I would like to know the difference between these two (sorry I do not know the name of this subject).

I come from C# where I was used to write System.data as well as classA.MethodA. I have already found out that in Cpp, with namespaces I need to use ::, with classmembers ->. But what about simple "."? I have created System::data:odbc::odbcConnection^ connection. Later I was able to use connection.Open. Why not connection->open?

Im sorry, I am sure its something easily findable on the net, but I dont know english term for these. Thank you guys

+4  A: 

If you have a pointer to an object, you use:

MyClass *a = new MyClass();
a->MethodName();

On the other hand, if you have an actual object, you use dotted notation:

MyClass a;
a.MethodName();
Pablo Santa Cruz
It would be worth mentioning the usage for C++/CLI "handles" also, because the question asks about this specifically.
Greg Hewgill
A: 

The short answer: C++ allows you to manage your own memory. As such, you can create and manipulate memory, through usage of pointers (essentially integer variables containing memory addresses, rather than a value).
a.Method() means a is an instance of a class, from which you call Method.
a->Method() means a is a pointer to an instance of a class, from which you call Method.

Traveling Tech Guy
A: 

When you use syntax like a->member, you are using a pointer to a structure or object. When you use syntax like a.member, you are using the structure or object and not a pointer to the structure or object.

I did a quick google for you and THIS looks fairly quick and decent explanation.

ChadNC
+2  A: 

To clarify the previous answers slightly, the caret character ^ in VC++ can be thought of as a * for most intents and purposes. It is a 'handle' to a class, and means something slightly different, but similar. See this short Googled explanation:

http://blogs.msdn.com/branbray/archive/2003/11/17/51016.aspx

So, in your example there, if you initialize your connection like:

System::Data::Odbc::OdbcConnection connect;
//You should be able to do this:
connect.Open();

Conversely, if you do this:

System::Data::Odbc::OdbcConnection^ connect1 = gcnew System::Data::Odbc::OdbcConnection();
connect1.Open(); // should be an error
connect1->Open(); //correct
Rooke
There's also a good discussion on the caret right here on Stack Overflow:http://stackoverflow.com/questions/202463/what-does-the-caret-mean-in-c-cli
Rooke