Thread: Class member access by *() ?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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をこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    5,108
    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