Thread: changing return type of derived function

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    changing return type of derived function

    Hi,
    I am writing a code and got a problem with deriving the class.
    I have got a class A (with pure virtual functions) which got a (pure virtual) function operator[].
    also I got classes B and C which inherits A.
    if B is a class which is a vector of complex numbers and C is a bitArray than what should operator[] return.
    in the situation of B it should return a reference to complex number.
    In the case of C it should return a proxy class which holds the bit.

    thus, how can i define operator[] in A so both B and C can overwrite the operator[].
    It is important that everyone who uses A knows that it supports operator[].

    thanks in advance.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't think you can do that.

    You'll have to have the same type for it to work correctly with inheritance. No escape from that.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    6

    can't i say that i just returning a reference to 'no matter what'?
    like a reference to Object in Java.
    something like void& (i just don't know if void& exists). ?
    thanks.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You can certainly return a reference to some base-class that in turn have virtual member functions, and return a derived class of that. But there is no way to say "i'm just returning 'any' object here" in C++ - you have to give it a type of some sort.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >like a reference to Object in Java.
    C++ doesn't have a top level type (unless you go out of your way to make one), nor does it have dynamic typing.

    >in the situation of B it should return a reference to complex number.
    >In the case of C it should return a proxy class which holds the bit.
    Why not use a proxy class across the board so that you can take advantage of polymorphism?
    My best code is written with the delete key.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You could return a void* pointer instead of a reference, but I'd highly recommend against doing that since you'd lose all type safety by doing that.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The proxy class approach is typically the safest in this situation, I would think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM