constructor syntax

This is a discussion on constructor syntax within the C++ Programming forums, part of the General Programming Boards category; Hello, I have the following constructor, Code: class COperator { public:COperator::COperator( HINSTANCE hInst ) : m_hDlg( NULL ), m_pTapi( NULL ...

  1. #1
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359

    constructor syntax

    Hello, I have the following constructor,

    Code:
    class COperator
    {
      public:COperator::COperator( HINSTANCE hInst ) : 
                                                      m_hDlg( NULL ),
                                                      m_pTapi( NULL ),
                                                      m_pCall( NULL )
                                                     {};
    };
    What I don't understand is the need for the '::' after the COperator. What is the difference when compared to:


    Code:
    class COperator
    {
      public:
        COperator( HINSTANCE hInst ) : 
                           m_hDlg( NULL ),
                           m_pTapi( NULL ),
                           m_pCall( NULL )
                           {};
    };
    If there are any advantages when it comes to using ::, it would be helpful to know.

    Thanks.
    Be a leader and not a follower.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >If there are any advantages when it comes to using ::, it would be helpful to know.
    There's no need for scope resolution when you're defining a member function within the scope of the class declaration. It's only needed when you separate the declaration and definition:
    Code:
    class COperator {
    public:
      COperator(HINSTANCE hInst);
    };
    
    COperator::COperator( HINSTANCE hInst )
      : m_hDlg( NULL )
      , m_pTapi( NULL )
      , m_pCall( NULL )
    {}
    My best code is written with the delete key.

  3. #3
    Registered User subdene's Avatar
    Join Date
    Jan 2002
    Posts
    359
    Ok thankyou. The code the was taken from a Microsoft sample, and I had never used the operator in that way. I have only used it when I sperate the code from class definition, i.e. using a class1.h and class1.cpp for example.
    Be a leader and not a follower.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    14
    reelated to the topic:
    this
    objetc o(10);
    is equivalent
    with
    object o = 10;

    how can i use "=" in this case
    object o(10, 20);
    ???
    thanks
    Making an operator (in your case the '=' sign) work with classes requires you to write an overloaded operator. The same operator can be overloaded multiple times for the same class, as long as the value on the right are of diffrent types. an example of this is cout.... when you type...
    Code:
    int x=1;
    cout<<x;
    the '<<' operator is overloaded for int's, but '<<' is overloaded for many diffrent types which is why you can do this as well..

    Code:
    char x='d';
    double y=3.14;
    string s="hello world";
    cout<<x;
    cout<<y;
    cout<<s;

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    objetc o(10);
    is equivalent
    with
    object o = 10;
    Only if the constructor isn't declared as explicit:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test1 {
      int i;
    public:
      test1(int init)
        : i(init)
      {}
    };
    
    class test2 {
      int i;
    public:
      explicit test2(int init)
        : i(init)
      {}
    };
    
    int
    main()
    {
      test1 a(10);  // Okay
      test2 b(10);  // Okay
      test1 c = 10; // Okay
      test2 d = 10; // Error! Implicit construction unavailable
    }
    how can i use "=" in this case
    object o(10, 20);
    You can't. It only works for constructors that take a single argument. You could manage something similar though by passing an array:
    Code:
    #include <iostream>
    
    using namespace std;
    
    class test1 {
      int i, j;
    public:
      test1(int init[])
        : i(init[0])
        , j(init[1])
      {}
    };
    int
    main()
    {
      int   args[] = {10, 20};
      test1 a = args;
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

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