Thread: Some questions about syntax

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Question Some questions about syntax

    Hi,

    I would like to know what the following mean and do:

    1. A function parameter with an assignment in the function declaration:

    Code:
    int someFunction (const string someString, bool somebool = false);
    What does that assignment (= false) mean and do?

    2. I'm building a C++ Berkeley socket wrapper and to access the close(socketDescriptor) function I need "::" in the front. What does "::" mean and do?

    Ie, I will have to do:
    Code:
    ::close(socketDescriptor)
    3.
    Code:
    someClass::someFunction(string &message, int var2) throw() : someString(message) {
    someString.append("??");
    }
    What does ": someString(message)" mean and do? Does it mean that at the end of this function someString will be thrown?

    Thanks,
    N29

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    1.
    A function parameter with an assignment in the function declaration:
    It's a default value that is assigned to the parameter variable if no corresponding argument is sent to the function. Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    void myfunc(int n, double d = 15.5)
    {
           cout<<n<<endl
                  <<d<<endl;
    		
    }
    
    int main()
    {
    	myfunc(10);
    	myfunc(10, 30);
    
    	return 0;
    }
    2. I'm building a C++ Berkeley socket wrapper and to access the close(socketDescriptor) function I need "::" in the front. What does "::" mean and do?
    "::" is the scope resolution operator, and when there is no name in front of it, it resolves to the global scope. Here is an example:
    Code:
    #include <iostream>
    using namespace std;
    
    const int number = 10;
    
    void myfunc(int number)
    {
    	cout<<number<<endl
    		<<::number<<endl;
    		
    }
    
    int main()
    {
    	myfunc(1);
    
    	return 0;
    }
    3.
    Code:
    someClass::someFunction(string &message, int var2) throw() : someString(message) {
    someString.append("??");
    }
    That code can be rearranged like this:
    Code:
    someClass::someFunction(string &message, int var2) throw() : someString(message) 
    {
    	someString.append("??");
    }
    throw() means the function is not allowed to throw any exceptions. By default, a function can throw any type of exception. throw() lets you specify which types of exceptions the function is allowed to throw between the parentheses. If you don't list any types between the parentheses, then the function can't throw any exceptions. Note: VC++6 does not support throw().

    The fragment:

    : someString(message)

    appears to be an initializer list, which is used in a constructor to initialize the member variables. I say "appears" because an intializer list like that is used with a constructor, so that would mean the actual code should look like this:
    Code:
    someClass::someClass(string &message, int var2) throw() : someString(message)
    Note the use of the scope resolution operator here to indicate the function you are defining is the one by that name in someClass.
    Last edited by 7stud; 05-15-2005 at 03:41 PM.

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Question

    Thank you

    I would like to ask why if we say:

    Code:
    someClass(int var1) throw(someException);
    are are allowed to throw an exception?
    Are we allowed because throw() has an arguement and the exception that is throw is of that arguement's class?
    and without an arguement, throw() would mean that no exception is allowed to be thrown inside that function/constructor?

    Thanks,
    N29

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    I just added something to my post, which happens to answer that specific question.
    Last edited by 7stud; 05-15-2005 at 03:40 PM.

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    Thanks again

    You are correct when you say:

    appears to be an initializer list, which is used in a constructor to initialize the member variables. I say "appears" because an intializer list like that is used with a constructor, so that would mean the actual code should look like this...
    It was part of a constructor!

    Is the initializer list only applicable to constructors or can it be to any function?

    Thanks,
    N29

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Only constructors. The initializer list initializes the member variables as the member variables are created. In addition, for some types of member variables, e.g. references, an intializer list is the only way you can initialize the member variables. It's good practice to initialize all your member variables in an intializer list to get used to doing that, and then if you have a member variable that is a reference type, you don't have to do anything different.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Hi newbie.
    About the initializers.. here's some info on them
    http://cboard.cprogramming.com/showt...749#post413552
    Calling
    Code:
    someclass::someclass(...) : arg_initializer(somearg){}
    only changes the constructor that is called.

    About ::
    :: is an operator which acesses symbols inside symbols with two conditions.
    Consider A::B
    A CAN'T be an instance of anything, therefore A must be a namespace or a class name, and never an instantiated object. So B can be anything that doesn't depend on any instance of anything: a namespace, a user defined type, a static variable, but not a non-static class variable.
    So if you have ::B (note thera is no A) , you're calling B from the global namespace. In other words, B doesn't belong to anything. It is a global symbol like any C function or global var.
    an example of this usage
    Code:
    #include <iostream>
    class X{public:X(){std::cout<<"X"<<std::endl;}}; // class a
    
    namespace v{
    	class X{public:X(){std::cout<<"v::X"<<std::endl;}};
    
    	void f(){
    		::X x1; // global X
    		X x2; //X from where we are - the namespace
    		
    		v::X x3; //X explicitly from the namespace
    	}
    }
    void f(){
    		::X x1;//global X
    		X x2; //X from where we are - global scope
    		
    		v::X x3; //X explicitly from the namespace
    	}
    
    int main(int argc, char* argv[]){
    	v::f();
    	f();
    	return 0;
    }
    //output
    X
    v::X
    v::X
    X
    X
    v::X
    Last edited by xErath; 05-15-2005 at 06:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM