Class member access by *() ?

This is a discussion on Class member access by *() ? within the C++ Programming forums, part of the General Programming Boards category; I have been looking over the Sockets C++ library and found a class member with the following member definition for ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Class member access by *() ?

    I have been looking over the Sockets C++ library and found a class member with the following member definition for a sockaddr.

    Code:
    virtual operator struct sockaddr *() = 0;
    This member is public, but I have *no* idea how to access it. Could somebody provide some insight on what this is?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    That is a conversion function. It allows you to convert an object of the class type (or in this case, a derived class type) to a struct sockaddr*, e.g., via a cast.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Ahhhh! Very cool, thank you! What is the advantage of using a conversion function versus a template, in this case?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Quote Originally Posted by wise0wl
    What is the advantage of using a conversion function versus a template, in this case?
    How would you use a template here? The alternative to a conversion function is to write a function with some other name, e.g., to_sockaddr_ptr, that performs the same conversion more explicitly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    3
    I realized that almost the second I posted it. Templating would make sense for defining the input method, not for doing the dynamic return typing (ie. conversion).

    Thanks!

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,546
    It should be noted that this opens up a bag of worms.
    I can take that class and do

    my_class a, b;
    a - b;

    Doesn't make sense, but because of that conversion operator, it works. It compiles.
    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.

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,231
    Doesn't make sense, but because of that conversion operator, it works. It compiles.
    It may or may not. It depends on the rest of the operators involved.

    Soma

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 08:42 AM
  2. private class member access
    By DarkMasterBosel in forum C++ Programming
    Replies: 6
    Last Post: 03-30-2008, 11:37 AM
  3. Replies: 25
    Last Post: 10-29-2007, 04:08 PM
  4. derived class can not access base class protected member?
    By George2 in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2007, 06:32 PM
  5. class member access denied
    By chiqui in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2002, 02:02 PM

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