Thread: Class operators when inheriting

  1. #1
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217

    Class operators when inheriting

    Hello. I'm making a class called "Packet" which will be used to build packet buffers for an online server that i'm coding for a game. The Packet class "inherits" a class called "ByteStream". The reason i can't just use ByteStream is because the protocol for the game is very strange and integers need to be formatted in a specific format before they can be writted/read.

    When i inherit a class, do i have to rewrite all the operators the class overloads? I think for some reason the compiler is calling the default "=" operator (where it does a shallow copy or whatever it's called) rather than calling the "=" operator defined in ByteStream. Heres what Packet looks like:

    Code:
    class Packet:
      public ByteStream
    {
    ...
    }
    So if i did this:

    Code:
    Packet packet;
    packet = Packet();
    Would that call ByteStream:perator=(const ByteStream&)??

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No (to the best of my knowledge).

    --
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it will, because Packet can be implicitly converted to ByteStream&.
    But take heed to make sure the operator will make sure it does what you want, because it will operate on the base object and not the derived object.

    Code:
    #include <iostream>
    
    using std::cout;
    using std::endl;
    
    class A
    {
    public:
    	A& operator = (A&) { cout << "A::operator =" << endl; return *this; }
    };
    
    class B: public A
    {
    };
    
    int main()
    {
    	B b1, b2;
    	b1 = b2;
    }
    Output:
    A::operator =
    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.

  4. #4
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Ok one more question. The constructors...I have to rewrite these right? Like do i have to do this?:

    Code:
        Packet():
            ByteStream(){}
    
        Packet(const char* data, size_t dataSize):
            ByteStream(data, dataSize){}
    
        Packet(const string& str):
            ByteStream(str) {}
    
        Packet(const char* str):
            ByteStream(str, strlen(str)){}
    
        Packet(const Packet& packet):
            ByteStream(packet){}
    
        template <class T>
        Packet(const T& value):
            ByteStream(value){}

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I suppose this is correct (you do have to rewrite the constructors).
    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.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    When i inherit a class, do i have to rewrite all the operators the class overloads?
    First of all, you do not inherit a class, but you derive a class (let's say B) from another (let's say A). This will make class B inherit all member functions from class A, including operator functions:

    13.5 Overloaded operators

    6. An operator function shall either be a non-static member function or be a non-member function and have at
    least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enu-
    meration. (...) Operator functions are inherited in the same manner as other base class functions.


    So if you don't override the inherited function, it'll call the operator function of the base class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 09:10 AM
  2. Using or inheriting a class
    By audinue in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 11-20-2008, 12:11 PM
  3. Overloading templated class operators
    By Artemis0583 in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2006, 12:30 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM