atmostmediocre
atmostmediocre

Reputation: 198

How to handle conflicting function and variable names?

I'm writing a simple socket C++ class and it looks like this:

class Socket {

public:
    // ...
    void connect();

private:
    // ...
    int socket;

};

There are two issues with my class:

  1. void Socket::connect()
    I'm using the native Linux socket interface, which means there is already a function called int connect(...) that I want to use inside the Socket::connect() function.

  2. int socket
    Same problem. The class variable name has a conflicting name with the int socket(...) function.

Question: Am I forced to change the name of the int socket and void connect() to something else, or is there a workaround?

Upvotes: 0

Views: 487

Answers (1)

R Sahu
R Sahu

Reputation: 206747

The global names can be used by using the global scoping operator, ::.

Inside the class, you can use ::connect and ::socket to use the global functions. To call the global functions, use ::connect(...) and ::socket(...).

Upvotes: 6

Related Questions