3

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

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Gustav
  • 63
  • 3

4 Answers4

4

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
  • 176,835
  • 32
  • 241
  • 292
  • It would be worth mentioning the usage for C++/CLI "handles" also, because the question asks about this specifically. – Greg Hewgill Nov 03 '09 at 18:02
2

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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
Rooke
  • 2,013
  • 3
  • 22
  • 34
  • 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 Nov 03 '09 at 18:43
0

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
  • 27,194
  • 23
  • 111
  • 159
0

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,528
  • 4
  • 25
  • 39