Inheritance from C struct

This is a discussion on Inheritance from C struct within the Networking/Device Communication forums, part of the General Programming Boards category; Hello all I wanted to create my own socket classes for future network programming in C++ and got disuaded shortly ...

  1. #1
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271

    Inheritance from C struct

    Hello all

    I wanted to create my own socket classes for future network programming in C++ and got disuaded shortly after, now I'm returning to it after solving a few problems. I do have one question to ask whilst carrying on.
    I am trying to add a few features to give programming flexibility without re-inventing the wheel so to speak.
    I was wondering if I could duplicate the sockaddr_in struct into a class in C++ and use it or can I derive classes that inherit from the struct itself. I once read somewhere that structs and classes were similar.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, the main difference between a class and a struct is that a class by default has private members, struct has global members.

    However, beware that a struct that has a constructor, copy-function or destructor, or virtual functions, is no longer considered "plain old data", which in turn means that some of the behaviour changes - particularly the reliance on the internal datastructure itself is now broken, because the compiler may insert "hidden" bits in the struct itself, which "breaks" the original data structure.

    What you could do is hide a sockaddr_in inside your class, and produce "extraction functions" that provide a sockaddr_in as part of the result from some functions.

    --
    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
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    While it is perfectly allowable to derive from a struct I would advise against it for the reasons matsp mentioned and also because normally when other programmer's see struct they expect it to be for a header or data block. Using struct to represent a class, while perfectly valid from a language standpoint, is going to make your card hard to follow later and perhaps hard to maintain. In my book using a struct as a class is nearly the same as overriding an operator to do something completely different than what the operator normally does. For instance overriding the + operator to write out a file or something.

    You could create a class that uses the struct to perform its duties which is a perfectly valid design decision.

  4. #4
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thank you both very much for your advise.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  5. #5
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Ok I'm back to having problems in Dev cpp with my socket programming code.
    I'm almost 100% certain it's to do with the code structure:
    Code:
    class sockID{
    private:
          string sockName, address;
          struct sockaddr_in cSock;
          unsigned short int *port;
    
    public:
           //the GET functions
           string getSockName() {return sockName; }
           string getSockAddress () {return address;}
           int getPort () {return (int) port;}
    
         //constructors
          sockID(string s_address, unsigned short int s_Port);
          sockID(string s_Name, string s_address, unsigned short int s_Port);
    };
    
    // Class constructors
    sockID::sockID(string s_Name, string s_address, unsigned short int s_Port) {
    					            
    		  port = &cSock.sin_port;
              sockName = s_Name;
              
              cSock.sin_family = AF_INET;
    		  cSock.sin_port = s_Port; // short, network byte order  MAY NOT WORK
    		  cSock.sin_addr.s_addr = inet_addr( s_address.c_str() );
    		  //inet_aton(s_address.c_str(), &(cSock.sin_addr)); <== not valid in windows
    		  memset(cSock.sin_zero, '\0', sizeof cSock.sin_zero);
    and then

    Code:
    int main() {
    //WSADATA wsaData; // if this doesn't work
    WSAData wsaData; // then try this instead
    if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) {
    fprintf(stderr, "WSAStartup failed.\n");
    exit(1);
    }
    
    rest of main..
    everything's in one file at the moment because this is a skeleton I'm building so I'd like to get it working before I tidy everything up into files.
    Basically I'm still getting the same: [Linker error] undefined reference to `WSAStartup@8' and [Linker error] undefined reference to `inet_addr@4' errors in dev cpp

    when I compile the file alone I get no errors whatsoever but when I compile the project it throws up these errors.
    My best guess is that maybe it's because I'm using those functions before the windows sockets initialisation routine.
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,437
    You still need to link with the winsock library.

    To the project options (alt-p), click on the parameters tab, then add the library libws2_32.a.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    WDT
    WDT is offline
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Thank you very much you are almost the God of C
    A hundred Elephants can knock down the walls of a fortress... One diseased rat can kill everyone inside

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 05:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21