Thread: need help with constructor

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    30

    Unhappy need help with constructor

    Code:
    //core unit test
    //given 2 constructors
    
    vector v1;
    vector v2 (5,3);
    
    //member functions :     int dotProduct(const Vector &x)
    
    int n = v1.dotProduct(v2)

    from the test core const Vector &x = v2
    so in order to use it in my dotproduct function i can simply use
    x.somefunct which refer to v2. but how can i access v1.let say if what i intend to do is

    Code:
    if (v1.somefunc != v2.somefunc){
    ...........;
    }
    should i copy both constructor? or save it in different memory?

    thanks in advance

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    v1 is this object in the method. You can just refer to the member's name, or put this-> in front of it:

    Code:
        somefunct();
        //or
        this->somefunct();
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    30
    so,is this right?
    im assuming what you are trying to say is this can be any object which in this case is v1 and x is v2 .

    Code:
    if (this->somefunct != x->somefunct){
    ....}
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-10-2008, 08:38 PM
  2. C++ have a constructor call another constructor
    By QuestionC in forum C++ Programming
    Replies: 4
    Last Post: 05-17-2007, 01:59 AM
  3. Replies: 3
    Last Post: 03-26-2006, 12:59 AM
  4. How do I override constructor in inheritance?
    By Loduwijk in forum C++ Programming
    Replies: 13
    Last Post: 03-24-2006, 09:36 AM
  5. Need help in classes
    By LBY in forum C++ Programming
    Replies: 11
    Last Post: 11-26-2004, 04:50 AM