Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

new'ing an array of classes.

Posted on 1998-05-11
7
Medium Priority
?
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 150 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

705 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