Thread: Pointers, addresses, and new

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    17

    Pointers, addresses, and new

    I'm having a simple yet confusing problem. Say SomeClass is a class with lots of methods and fields and that uses multiple inheritance (could that be the problem ?):
    Code:
    somearray[index] = new SomeClass();
    On compilation, I get the following error :
    no match for 'Character & = Character *'
    candidates are: class Character & Character:perator =(const Character &)

    So now I understand I have an address on the left and a pointer on the right side, and that I can't assign one to the other since they're not of the same type. But I still can't
    #1: Solve this problem (tadaa!)
    #2: Plainly understand what the second entry in the error log means.

    Anybody cares enough to help me ? Thanks for your time.
    Using Dev-C++ beta under Win XP Pro. That or g++ on Mandrake Linux 9.0 .

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    somearray should be defined:
    Code:
    SomeClass **somearray;
    //OR
    SomeClass *somearray[A_SIZE];
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    max1234
    Guest
    ok, from reading what you wrote :

    Code:
    somearray[index] = new SomeClass();
    I know that somearray is defined like this (100 is arbitrary):
    Code:
    SomeClass somearray[100];
    this means somearray is holding REFERENCES of SomeClass, not pointers. this is a difference between C and C++.
    On another hand, the operator new ALWAYS returns a POINTER, not a reference.

    so, your code is trying to assign a pointer to something that's a reference - ie an object - , to do so, the compiler is trying to find out if you overloaded the operator assignement (=) and found a candidate that does not match this one: that's the second line of the compiler error.

    don't pay attention to XSquared. what he sayd doesn't make sense.

    what you should do is :
    Code:
    SomeClass *somearray = new SomeClass [100];
    somearray[index] = new SomeClass();
    that's how it goes.

    on another hand, if you wrote :
    Code:
    SomeClass somearray[100];
    you have just constructed 100 object of type SomeClass.

    alright, that's it. hope it helped

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    No, because new returns a pointer, so you need an array of pointers.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    XSquared is right. It is right now, an array of pointers.

    I'm not sure what exactly you're looking for, but I'm guessing you're looking for something like this:
    SomeClass* somearray = new SomeClass[size];
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    max1234
    Guest
    yeah you're right, i don't know what i was thinking.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    17
    Yup, XSquared, you were most right.
    Code:
    SomeClass *somearray[A_SIZE];
    did the job right. I should've known better... I should stop reading and start coding, really. Thanks a lot.
    Using Dev-C++ beta under Win XP Pro. That or g++ on Mandrake Linux 9.0 .

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Also, you should be aware that C++ provides better ways (safer ways) to handle dynamic arrays, and pointers. Ideally you'd use a vector of either objects or smart pointers to objects rather than a pointer to an array of pointers to objects.

    Just today, in fact, I wrote a (rather long) post for someone who was doing just about the same thing as you. In general, robust C++ code will tend to use pointers and arrays sparingly; a pointer to an array of pointers to objects has to be managed *very* carefully if you care about writing robust code. Memory leaks can occur both through malformed code and through exceptions.

    Of course, if your program is not crucial, it may well be OK to simply allow exceptions to crash the program, in which case exception handling isn't an issue. And memory leaks from malformed code, which will always be freed when the process dies, are often OK, so long as they're small enough, infrequent enough, and the process terminates before the cumulative effect becomes important. However, it's still preferable to write code that doesn't leak memory, or crash on an error (exceptions are not a very graceful way to die). And so, rewriting code to stay away, when possible, from dynamically allocated arrays and "dumb" pointers can help you program smarter.

    The best part (IMO) is that you get these benefits, PLUS it takes less code! So you win twice, and the performance is (very, very nearly) as good as the array.

    You can read my (long) post on the same topic here:
    http://cboard.cprogramming.com/showt...5&pagenumber=2

Popular pages Recent additions subscribe to a feed