Solved

new'ing an array of classes.

Posted on 1998-05-11
7
219 Views
Last Modified: 2013-12-14
Is this even possible?  Eg:
float *fp = new float[10];  // allocate an array of type float

class NewClass
{
...
}

NewClass *classArray = new NewClass[10];    // Is this possible?

Compiling and running this in the debugger MS VC++ 5 shows that the last line news a single object of class NewClass and the trailing [10] seems to have been dispensed with.  Trying to access a member function of classArray[0] produces a compile error.

classArray[0]->MemberFunction();

F:\Espro For Windows\Source\matworksht.cpp(152) : error C2819: type 'ClassArray' does not have an overloaded member 'operator ->'
F:\Espro For Windows\Source\matworksht.cpp(152) : error C2227: left of '->MemberFunction' must point to class/struct/union

0
Comment
Question by:spearhead
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 50 total points
ID: 1175853
It should be

classArray[i].MemberFunction();

or

(classArray + i)->MemberFunction();
0
 

Expert Comment

by:shash
ID: 1175854
In the statement,

classArray[0]->MemberFunction();

the moment u use classArray[0], it means *(classArray + 0). In other words the classArray pointer has been dereferenced. So the right way to invoke the MemberFunction() is

classArray[0].MemberFunction();

The error, "No overloaded member 'operator ->'" is correct but is misleading.
0
 

Expert Comment

by:shash
ID: 1175855
This is what Sun C++ 4.2 compiler has to say about the error.

"Error: Pointer type needed instead of NewClass"

which ofcourse is more helpful than the one by MSVC++ compiler.

0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:spearhead
ID: 1175856
shash

I was actually guessing at a minor problem with the MSVC++ compiler as there is no real difference between classArray[i] and (classArray + i) as it is all just pointer arithmetic.  The [] notation is (IMO) a lot easier to read and therefore maintain so I have elected to go with that.  The MSVC++ error message sent me down the wrong track when trying to interpret the problem.

Is there a way to split the points on this one?  I don't really want to fail chensu as his answer was (technically) correct however your solution appears to be a better one.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175857
shash .. the VC messages actually cover mode cases.

There would be nothing wrong with
  classArray[0]->MemberFunction();
IF NewClass has an operator-> defined.  For exmaple, NewClass might be a smart pointer class.  In this case, one assumes that NewClass does NOT have an operator->, hence the first error message.

After determining that there is no operator-> override, VC is assuming that you mean the default ->.  The second message VC reports says that the LHS of the -> needs to be a pointer to a struct, class or union.  Which is also exactly right.

So the two message taken together cover both cases and are not at all misleading when you appreciate the two potential problems here.

If your problem was that you had forgotten the operator->, then the first message would be very helpful indeed.

0
 

Author Comment

by:spearhead
ID: 1175858
Uh Oh!  Now I am not sure which answer is the most correct.  No, NewClass is NOT a smart pointer class so on allocation, classArray (and hence classArray[0]) should simply be a pointer to the initial object instantiated.

(classArray + 1)->MemberFunction()
as suggested by chensu, and so does
classArray[1].MemberFunction()
which, after thinking about it, I should have known that classArray[1] would not be a pointer, but an object in much the same way as string[1] is a char even though tring may be a char *.

I will award the points to the original answer suggested and hopefully this is not seen as unfair with regard to shash 's answer

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1175859
spearhead .. my comments were to explain what the error message you received meant, as shash claimed they weren't helpful.

We are all saying the same thing .. just adding to the explaination.

you just used a '->' where you should have used a '.'

Its as simple as that

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question