Tom's Hardware > Forum > Applications > Programming > [Solved] Another C++ question, pointers and method calls.

[Solved] Another C++ question, pointers and method calls.

Forum Applications : Programming - [Solved] Another C++ question, pointers and method calls.

Tom's Hardware: Over 1.4 million members in 6 different countries available to answer all your high-tech questions. Sign up now! Its free!

Best answer from mindless728.

Word :    Username :           
 

I'm back again with another question about C++.

It is my understanding that there are a few ways to create objects in c++. The first way would be to just declare it (such as: "Date testDate(8, 23, 1989);" ), and that would create it on the stack.

The second way to do it is using the "new" operator, which puts it in the heap memory, and requires a pointer to access it. My question is, how would you call methods on an object stored in the heap memory?

I have a test class that stores the Date. One of it's methods is GetYear();. When i use this method on the variable stored in the stack, it works. But when i try to run it on the pointer, i get "Date.cpp:25: error: request for member ‘GetDate’ in ‘pDate’, which is of non-class type ‘Date*’".

Is there some special way i have to call a method on a pointer to get it to work? Or do i have to create a seperate method in the Date class that uses pointers?

Thanks :sol:

there are two ways

Code :
  1. //first way
  2. Data * pDate = new Data(8,23,1989);
  3. (*pDate).GetDate();
  4. //second way
  5. Data * pDate = new Data(8,23,1989);
  6. pDate->GetDate();



basically you can either deference the object first then use the method call (first way) or use the arrow that deferences for you and does the method call

Sponsored Links
Register or log in to remove.
Best answer

there are two ways

Code :
  1. //first way
  2. Data * pDate = new Data(8,23,1989);
  3. (*pDate).GetDate();
  4. //second way
  5. Data * pDate = new Data(8,23,1989);
  6. pDate->GetDate();



basically you can either deference the object first then use the method call (first way) or use the arrow that deferences for you and does the method call

Reply to mindless728

Worked like a charm. Thanks again mindless

Reply to kyeana

no problem, i go to college for programming (computer science)

Reply to mindless728

:D I took this semester off, but am also doing computer science in college. I know java pretty well, but figured i would get a jump start on C++, as that will be coming up soon.

Reply to kyeana

i started at a community college with c++, learned java, and a few asm languages (x86, m68k, z80) and transfered to a 4yr this fall

though try http://www.cplusplus.com/ pretty good resources to learn and look up some c++ programming stuff

Reply to mindless728

I will go check that out. Thanks for the info.

Reply to kyeana
Tom's Hardware > Forum > Applications > Programming > [Solved] Another C++ question, pointers and method calls.
Go to:

There are 724 identified and unidentified users. To see the list of identified users, Click here.

Sponsored links
  • Ask the community now
  • Publish
Ad
They won a badge
Join us in greeting them