+ Reply to Thread
Results 1 to 8 of 8

Thread: Today's C++ question

  1. #1
    Join Date
    May 2005
    Posts
    390

    Default Today's C++ question

    Probably an easy one.

    Class CConnectionPointContainer has the following code:
    CConnectionPoint *Points; // private member

    Points = (CConnectionPoint *)CoTaskMemAlloc(10 * sizeof(LPCONNECTIONPOINT)); // in ctor

    A method in CConnectionPointContainer does this:
    Points[cPoints - 1] = new CConnectionPoint(this, cPoints);

    MSVC barfs.
    Code:
    CConnectionPointContainer.cpp(30) : error C2679: binary '=' : no operator found which takes a right-hand operand of type 'CConnectionPoint *' (or there is no acceptable conversion)
    
            c:\code\minitroll\CConnectionPointContainer.h(51): could be 'CConnectionPoint &CConnectionPoint::operator =(const CConnectionPoint &)'
    
            while trying to match the argument list '(CConnectionPoint, CConnectionPoint *)'
    Is it telling me I need a copy constructor? Do I need a copy constructor? It just occurred to me that the CoTaskMemAlloc call isn't 100% correct--I should be asking for 10 * sizeof(CConnectionPoint *), but in this case pointers are pointers.

    My intention was to allocation 10 pointers worth of memory, and whenever I'd new up a CConnectionPoint, store a pointer to it in Points, then treat Points as if it were an array.

    If I asked for 10 * sizeof(CConnectionPoint) then I'd definitely want to be using the copy ctor, because I'd be copying the new'd points into the array. I don't like that, though, because then I might as well immediately delete the new'd point, and later, freeing up the array would be odd: I couldn't use delete, I'd need to use CoTaskMemFree (you can think of these like malloc and free, but they're not exactly) to give back the whole array, and I don't think that would call the dtors, not that I've tried it yet.

  2. #2
    Join Date
    Mar 2005
    Location
    California
    Posts
    2,099

    Default

    Quote Originally Posted by Pix View Post
    Probably an easy one.
    Not really, heh

    I think your problem is here:
    Points[cPoints - 1] = new CConnectionPoint(this, cPoints);
    new returns a void*, and you are trying to shoehorn that into a Points[]. The array notation will cause that to be interpreted as a CConnectionPoint, so what you are trying to do is shove a void* into a CConnectionPoint, and thus the compiler is telling you there is no such conversion possible (which there isn't).

    I think if you want to use Points as an array, what you want is a pointer to a set of pointers, so your definition should be:

    CConnectionPoint **Points;

    Then, when you use Point[], it will be interpreted as a CConnectionPoint*, and the compiler will put the void* into it.

    I think. When you start getting into pointers to pointers, it begins to make my head hurt. But if I interpret what you are trying to do correctly, I think that's what you are after.
    "They laughed when I said I was going to be a comedian ... They're not laughing now." - Bob Monkhouse

  3. #3
    Join Date
    May 2005
    Posts
    390

    Default

    Ok, I'll try that.

    It occurs to me I probably meant assignment operator where I wrote copy constructor.

  4. #4
    Join Date
    May 2005
    Posts
    390

    Default

    Wait, operator new returns a void *?

    That means that
    T *x = new T();

    would always require a cast, and it doesn't, as far as I know.

  5. #5
    Join Date
    May 2005
    Posts
    390

    Default

    Ok, I made a few changes that appear to work.

    Points is now defined as CConnectionPointContainer **, and I cast the memory returned by CoTaskMemAlloc to same.

  6. #6
    Join Date
    Mar 2005
    Location
    California
    Posts
    2,099

    Default

    Quote Originally Posted by Pix View Post
    Wait, operator new returns a void *?

    That means that
    T *x = new T();

    would always require a cast, and it doesn't, as far as I know.
    Yes, it returns a void*. See top of this page:
    http://www.cplusplus.com/reference/s...perator%20new/

    And you are right, it doesn't require a cast back to a normal pointer. I don't honestly know why. In C that's legal (it's an implicit cast). In C++ it should be required. Unless there is some chicanery going on within the new() command.
    "They laughed when I said I was going to be a comedian ... They're not laughing now." - Bob Monkhouse

  7. #7
    Join Date
    Mar 2005
    Location
    California
    Posts
    2,099

    Default

    Just as a quick followup to this, because just by amazing coincidence, I'm going to be working on new/delete overrides soon (and thus I am doing research in this area).

    It is confirmed that new returns a void*. See, for example, this page:

    http://www.informit.com/guides/conte...plus&seqNum=40

    So that still leaves the question as to why a new command doesn't require an explicit cast. For example, this works fine

    int* Pointer = new int;

    Why does the compiler not throw an error here? In C, that is legal, but in C++ it is not (casting from void* to any other pointer type requries an explicit cast (generally a static_cast). I've also confirmed that with a quick test myself.

    void* SomeFunc()
    {
    void* Pointer; return Pointer;}

    int* IntPointer = SomeFunc();

    That gives a compiler error.
    cannot convert from 'void *' to 'int *'
    1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    So what gives? Well, we know the operator new is actually static, so the prototype is actually

    static void* operator new (size_t size);

    But making that test function static doesn't help us any, we get the same error. So, the only explanation I have is the "operator" part. When I override the new function in a class, no cast is needed. I can't really confirm if that's something general that applies to all "operator" overloads or if it's specific to the new operator, because no other operator returns a void*.

    So anyway, there you go.
    "They laughed when I said I was going to be a comedian ... They're not laughing now." - Bob Monkhouse

  8. #8
    Join Date
    May 2005
    Posts
    390

    Default

    I have to assume compiler magic.

+ Reply to Thread

Similar Threads

  1. Today's awesomely obscure C++ code
    By Pix in forum General Chit Chat
    Replies: 2
    Last Post: 01-15-2009, 01:12 PM
  2. C++ ctor question
    By Pix in forum General Chit Chat
    Replies: 6
    Last Post: 04-17-2008, 07:39 AM
  3. STL question
    By Pix in forum General Chit Chat
    Replies: 2
    Last Post: 04-30-2007, 04:24 PM
  4. The question from a new user
    By GRIENlord in forum General Chit Chat
    Replies: 6
    Last Post: 04-17-2007, 03:49 AM
  5. DDO Char Gen Question
    By Seipherwood in forum Character Planner
    Replies: 6
    Last Post: 11-30-2006, 01:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts