Results 1 to 3 of 3

Thread: Best way to pass a of vector<class> to a method of another class

  1. #1
    Join Date
    Sep 2010
    Posts
    654
    Thanks
    56
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Best way to pass a of vector<class> to a method of another class

    I need your help:
    My questions are related to my program can be as fast as possible.

    I have :
    1.- I create n instances of class1.
    2.- vector to save this instances :
    First question what is better to save pointers or reference - copies ?

    3.- I want to pass this vector to a function of class2
    Second question what is better to pass pointer to vector or reference.

    4.- In this new function I'm going to create a vector of a new class
    vector<class3> and I know the exact number of elements I have to create for this vector.

    5.- I'm going to pass the instances of class1 that I've received to this vector, trought a method. So I have for class3 a method called set_class1.
    Third question what is better to pass to this set_class1 pointer or reference, taking into account that I have received the class1 objects using a vector.

    The code I have now : (pseudocode )


    Code in main:

    Qt Code:
    1. vector<My_class1> * v_my_class1 = new vector<My_class1>;
    2. for (int i=0;i<limit;i++)
    3. {My_class1 a_class1 = new My_class1;
    4. v_my_class1->push_back(*a_class1);}
    5.  
    6. My_class2(v_my_class1) ;
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. My_class2(vector<My_class1> * a_vector_of_class1)
    2.  
    3. vector<class3> v_vector_class3 ;
    4. for (int i=0;i<a_vector_of_class1->size();i++)
    5. { v_vector_class3.push_back (*(new class3));
    6. v_vector_class3[i].set_class1(&a_vector_of_class1->at(i) }
    To copy to clipboard, switch view to plain text mode 

    ( a_vector_of_class1->at(i) compile a_vector_of_class1[i] not ... ???)


    Any advise ? Thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Best way to pass a of vector<class> to a method of another class

    ( a_vector_of_class1->at(i) compile a_vector_of_class1[i] not ... ???)
    try with
    Qt Code:
    1. (*a_vector_of_class1)[i]
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

  3. #3
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Best way to pass a of vector<class> to a method of another class

    There is no difference in performance between passing pointers or references; they are, in fact, identical operations when compiled. Passing by reference is syntactically "cleaner" than passing by pointer, since you don't have to explicitly dereference the object, but you aren't going to see any improvement in execution speed either way.

    Have you considered taking an introductory course in C++, or programming in general? You are spending a huge amount of time here pursuing very basic concepts, and in many cases they don't seem to be sinking in. You are going to wind up with a patchwork, imperfect understanding of a limited subset of programming/C++/Qt that will do you more harm than good in the long run.

Similar Threads

  1. Replies: 3
    Last Post: 9th February 2011, 07:28
  2. How to call super class method from sub class
    By mkkguru in forum Qt Programming
    Replies: 9
    Last Post: 4th February 2010, 05:29
  3. vector with own class
    By T0bi4s in forum Qt Programming
    Replies: 13
    Last Post: 14th January 2010, 23:06
  4. Replies: 3
    Last Post: 16th May 2007, 11:07
  5. How to pass a QString to another class ?
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 20:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.