Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ scope resolution operator ::

Options
  • 18-12-2013 11:52am
    #1
    Registered Users Posts: 839 ✭✭✭


    Hi Guys,
    I'm trying to understand a chunk of code. It's a C++ project, with tons of files working together. The basic concept is bundling a message up for transmission. I've been able to follow the progression to a certain point but now I'm stuck. See below for a rough outline of where I'm stuck....

    using namespace std;
    #include <headerfile1.h>
    #include <headerfile2.h>

    namespace test

    void TestApp::send(vector<unsigned char> b)
    {

    int n = ::send(socket_, (const char*)buf, int c);
    }

    I've left out alot of the internal stuff of the send() method, which I think isn't relevant.
    MY query is, where do I go to look for the other send method, mentioned inside the function, which is called using ::send. I need to locate it to make some modifications, and I've been going through loads of .cpp files, but can't find it.
    I know :: is a scope operator, and roughly it means "one level back" - but I can't think where to go searching for it.

    Hopefully you guys can help - I realise it may be difficult if you're not looking at the full setup I have

    thanks


Comments

  • Registered Users Posts: 13,080 ✭✭✭✭Maximus Alexander


    The scope operator :: is used to access a hidden name. In this case, you've defined a method called send(), so in order to access a method of the same name in another file you need to use the scope resolution operator to tell the compiler that you're referring to the other send() method, which looks like it will send information over a socket.

    Based on the code you've supplied, this must be in either headerfile1.h or headerfile2.h.


  • Registered Users Posts: 839 ✭✭✭kelbal


    The scope operator :: is used to access a hidden name. In this case, you've defined a method called send(), so in order to access a method of the same name in another file you need to use the scope resolution operator to tell the compiler that you're referring to the other send() method, which looks like it will send information over a socket.

    Based on the code you've supplied, this must be in either headerfile1.h or headerfile2.h.

    Thanks Maximus, yes I reckoned that its a send method in the header files - there are more header files mentioned than just 2 of them. I'll have to keep digging! Thanks though, clarifies what I suspected.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Should never put "using namespace std" above the headers. If the headers depend on this being there then that is very bad!!

    Also passing in the entire vector by value is probably not efficient, pass in a reference instead otherwise you are just copying the entire thing.

    Should be rather:

    #include <headerfile1.h>
    #include <headerfile2.h>

    using namespace std;

    Assuming the code actually compiles, you can open it up with an IDE (eclipse or visual studio or whatever) and just search for all references to send, or jump to definition.

    I suspect the send() call you are looking for is a system API call in <sys/socket.h>. So it may not be part of your project. If it's for windows then it's a similar call in <winsock.h>.

    http://linux.die.net/man/2/send
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms740149(v=vs.85).aspx


  • Registered Users Posts: 839 ✭✭✭kelbal


    srsly78 wrote: »
    Should never put "using namespace std" above the headers. If the headers depend on this being there then that is very bad!!

    Yes, thats the way it is in the proper code, just a typo on how I transcribed. The vector is passed by reference too.
    srsly78 wrote: »
    I suspect the send() call you are looking for is a system API call in <sys/socket.h>. So it may not be part of your project. If it's for windows then it's a similar call in <winsock.h>.

    You're on the ball there in terms of the send function I think. These headers are included......

    #ifndef WIN32
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <netinet/in.h>
    #include <netinet/tcp.h>

    I'm not 100% sure where the filepath is for that socket.h though

    thanks


  • Registered Users Posts: 839 ✭✭✭kelbal


    srsly78 wrote: »
    Assuming the code actually compiles, you can open it up with an IDE (eclipse or visual studio or whatever) and just search for all references to send, or jump to definition.

    I'm using eclipse. So, If I have a method that is being called (send() in this case), I can use eclipse to tell me where that particular method is located in the file system?


  • Advertisement
  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Yes just right click on function. However this is a system api call, so you will end up looking at sys/socket.h.

    Here is your function: http://linux.die.net/man/2/send


  • Registered Users Posts: 839 ✭✭✭kelbal


    thanks srsly78
    I've searched the filesystem and I see more than one sys folders with socket.h in it. How do I know which is the correct one? If I right click on the ::send function, what do I then select? Does it need to be highlighted?


    thanks


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    That's just the header. The actual code is just a C wrapper that calls into the kernel.

    https://sourceware.org/git/?p=glibc.git;a=blob;f=socket/socket.c;h=1f162a49c1f0ac0a38a228fb2e922af4bb5d18c4;hb=HEAD

    If you REALLY want to see the code then you have to look at kernel source. If this is an exercise in curiousity then all good, but if you just want to know how to use the function then read the documentation linked earlier.

    Here you go: http://lxr.linux.no/linux+v3.9.4/net/socket.c


  • Registered Users Posts: 839 ✭✭✭kelbal


    I get ya. So that socket.h (and corresponding .cpp file) aren't things that you really want to go messing around with. They're just a standard means of transmitting a chunk of data to another listening application.
    I guess whats then, is that there's a host and port specified as part of that socket - and if I can locate the program thats listening on this ip/port, and I can figure out where this send() function is sending its data to. I may have an idea where to look for this.


  • Registered Users Posts: 7,157 ✭✭✭srsly78


    Linux kernel is written in C, not C++ - so it's socket.c, not socket.cpp :p

    Try putting a breakpoint there and debugging it when it's running, then you can inspect the variables and see what the target is etc.

    http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.cdt.doc.user%2Ftasks%2Fcdt_t_add_brkpnts.htm


  • Advertisement
  • Registered Users Posts: 2,022 ✭✭✭Colonel Panic


    kelbal wrote: »
    I get ya. So that socket.h (and corresponding .cpp file) aren't things that you really want to go messing around with. They're just a standard means of transmitting a chunk of data to another listening application.
    I guess whats then, is that there's a host and port specified as part of that socket - and if I can locate the program thats listening on this ip/port, and I can figure out where this send() function is sending its data to. I may have an idea where to look for this.

    From the naming convention, I'd imagine socket_ is a member of the TestApp class, so it's probably being created there in it's constructor, an init method or maybe something called "Connect".

    Maybe that's where the IP and port are being specified.


Advertisement