Thread: need explanation

  1. #1
    Registered User Ssfccsh's Avatar
    Join Date
    Sep 2003
    Posts
    11

    need explanation

    while going through an example, i came across this:

    Code:
    customer::customer(char *firstname, char *secondname, int m , int d, int y)
           
           :dateofbirth(m, d, y)
    
                  {
                         ...;
                  }
    My guess is that the code in red is a constructor for the class dateofbirth. This in turn lead to my conclusion that the class dateofbirth is a private member of the class customer. Further more, since it is written that

    Code:
    customer::customer(char *firstname, char *secondname, int m , int d, int y)
    This would also means that in the main function, i am able to do sthg like this

    Code:
    void main(void)
    {
           ...
           customer customer1;
    
           customer1(ssfccsh, ssfccsh, 1, 1, 2004)
    }
    Am i right?

    Another doubt that needs clarification is that is it always true that const member function can only access const class member and static member function can only access static class member? If an object of a class is declared const, does it mean that all its class members are all const and any call to the member function in the class would be illegal?
    Last edited by Ssfccsh; 03-01-2004 at 02:35 AM.
    Dun ask what

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> My guess is that the code in red is a constructor for the class dateofbirth.
    "dateofbirth" is either an instance of a class or struct which accepts a contructor with three integer parameters, or it's a base class of "customer"

    >> This in turn lead to my conclusion that the class dateofbirth is a private member of the class customer.
    If it is a member, there is nothing to suggest that it's private, protected, or public.

    >> customer customer1;
    >> customer1(ssfccsh, ssfccsh, 1, 1, 2004);
    Does it compile?

    If you have a const object, you can read any public member and you can call any const method. Inside a const method, you can read any member. If you have a const object or you're inside a const method, you can't write-to/modify the object unless the member you're modifying is declared with the "mutable" keyword".

    You can use your compiler to explore all these questions.

    gg

  3. #3
    Registered User Ssfccsh's Avatar
    Join Date
    Sep 2003
    Posts
    11
    Code:
    customer &anyfunction(customer &);
    
    customer &anyfunction(customer &input)
    {
           ...;
           return input;
    }
    what is the scope of input?
    If the scope of input is local, would customer2 = customer1 still be valid once anyfunction is exited?

    Code:
    customer2 = anyfunction(customer1);
    Dun ask what

  4. #4
    Registered User Ssfccsh's Avatar
    Join Date
    Sep 2003
    Posts
    11
    (double post deleted)
    Last edited by Ssfccsh; 03-01-2004 at 10:25 AM.
    Dun ask what

  5. #5
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    (double post)

    well, unless you actually are creating a new customer inside of anyfunction, all you are returning is a pointer to customer1 and setting it to customer2, so customer2 points to customer1 basically.

  6. #6
    Registered User Ssfccsh's Avatar
    Join Date
    Sep 2003
    Posts
    11
    In that case, can i say that &input = customer1 is the same as *input = &customer1 where input is customer1 itself (since input is not a new variable but just another name for customer1)?
    Dun ask what

  7. #7
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    New York
    Posts
    1,905
    Originally posted by Ssfccsh
    In that case, can i say that &input = customer1 is the same as *input = &customer1 where input is customer1 itself (since input is not a new variable but just another name for customer1)?
    if you wanted to make input point to customer, you would do something like this:

    customer *input=&customer;

    and then you could do input->anyfunction(&customer);

    if you are trying to pass a pointer, and directly copy over the data, you would have to do something like:

    Code:
    customer *anyfunction(customer *input)  // Pass pointer to a customer and return a pointer to a customer
    {
           customer tempCustomer;
           tempCustomer.SetSomeValues(34,255);
    
           *input=tempCustomer;    // Copy the whole customer data over to our value that we passed.
    
           return input;   // return the same pointer that we passed
    }
    -hope that helps a bit

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked Lists
    By Bleu_Cheese in forum C++ Programming
    Replies: 13
    Last Post: 12-21-2007, 09:17 PM
  2. printf function explanation
    By vice in forum C Programming
    Replies: 3
    Last Post: 09-21-2005, 08:35 PM
  3. Explanation
    By fallonides in forum C Programming
    Replies: 3
    Last Post: 03-19-2003, 01:41 PM
  4. basic linked list declaration..need explanation
    By aspand in forum C Programming
    Replies: 3
    Last Post: 06-07-2002, 05:55 PM
  5. Explanation!!!
    By MITCHELL in forum Windows Programming
    Replies: 4
    Last Post: 10-25-2001, 01:13 AM