help!

This is a discussion on help! within the C++ Programming forums, part of the General Programming Boards category; in a C++ program, if I want to do this... "Class1" { ... Class2 *node1 = new Class2(); Class2 *node2 ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    101

    help!

    in a C++ program, if I want to do this...
    "Class1"
    {
    ...
    Class2 *node1 = new Class2();
    Class2 *node2 = new Class2();
    Class2 *node3 = new Class3();
    if (node1->method(node2)==node3) {...}
    ...
    }

    "Class2"
    {
    Class2 Class2::method(Class2 node)
    {
    ...
    return node;
    }

    What should the references put in the return type of method?? the argument?? and the last code "return node"??
    Such as,
    Class2 * Class2::method(Class2 * node)
    {
    return * node;
    }
    this is not correct. Would anybody help me to fix this problem?? thank you very very much!

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    19
    since the return type is 'Class2*', which is a 'pointer to a class', the return value must reflect this. Therefore, this should be:

    return node;

    however, i'm not sure what you're trying to do here. are you trying to compare the addresses of the class objects? if so, this code is syntactically correct, but will not function.

    also, don't forget to clear up memory allocated via the 'new' operator with:

    delete node1;
    delete node2;
    delete node3;

    Regards,
    Peter Kimberley

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Really thankful. However, let me simplify my questions.
    1. If a method(in Class1) returns type is "Class2", the reference in the method should be '*' or '&', also for the node that's returning from method?
    Class2 ? Class1::method(){...return ? node;}
    2. If the argument of method is a *node of Class2, what should the the reference be again, '*' or '&'?
    Class1 *object1 = new Class1();
    Class2 *object2 = new Class2();
    object1->method(object2);
    -----------------------------------------
    Class1::method(Class2 ? node){...}
    Actually, I'm always confusing with the '*' and '&' in C++. Hopefully, you can solve my problem. Thank you very much!!
    Last edited by DramaKing; 10-15-2001 at 05:10 PM.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    101
    Really thankful. However, let me simplify my questions.
    1. If a method(in Class1) returns type is "Class2", the reference in the method should be '*' or '&', also for the node that's returning from method?
    Class2 ? Class1::method(){...return ? node;}
    2. If the argument of method is a *node of Class2, what should the the reference be again, '*' or '&'?
    Class1 *object1 = new Class1();
    Class2 *object2 = new Class2();
    object1->method(object2);
    -----------------------------------------
    Class1::method(Class2 ? node){...}
    Actually, I'm always confusing with the '*' and '&' in C++. Hopefully, you can solve my problem. Thank you very much!!

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