How to pass main object reference to its all low level modules in c++

When ever I create my main(Car) object, internally its creates low level modules one by one.

I am referring some function in main object in all internal object

Currently I am passing main object through function(Setter function in all class)

Some how I don't feel its good.could you suggest me some good design.

example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
car mainobject
 
   grid object                mainobject->getFIleName()
      vertex oject            mainobject->getFIleName()
          sub object1         mainobject->getFIleName()
          sub object2         mainobject->getFIleName()
 
   triangle object             mainobject->getFIleName()
        vertex oject            mainobject->getFIleName()
          sub object1           mainobject->getFIleName()
          sub object2            mainobject->getFIleName()
 
   canvas ojbect                mainobject->getFIleName()
 
   mesher oject                 mainobject->getFIleName()
         obj1                          mainobject->getFIleName()
         obj2                           mainobject->getFIleName()
         obj3                             mainobject->getFIleName()



I though of have static class. from static class I will get main object but i need to create multiple instance of car so I cant use static class
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <typeinfo>
#include <iomanip>

struct car ;

struct component // life-time of a component does not exceed the life-time of the main car object
{
    explicit component( const car& c ) : main_object(c) {}
    virtual ~component() = default ;
    // defaulted copy/move constructor, copy/move assignment

    void print_file_name() const ;

    // ...
    const car& main_object ;
};

struct some_sub_object : component
{
   using component::component ; // inherited constructor http://www.stroustrup.com/C++11FAQ.html#inheriting
   // ...
};

struct vertex : component
{
   explicit vertex( const car& c ) : component(c), one(c), two(c)  {}
   // ...
   some_sub_object one ;
   some_sub_object two ;
};


struct grid : component
{
   explicit grid( const car& c ) : component(c), the_vertex(c)  {}
   // ...
   vertex the_vertex ;
};

struct triangle : component
{
   explicit triangle( const car& c ) : component(c), the_vertex(c)  {}
   // ...
   vertex the_vertex ;
};

struct car
{
    explicit car( const std::string& file_name ) : path_to_file(file_name), mesh(*this), shape(*this) {}

    const std::string& file_name() const { return path_to_file ; }

    std::string path_to_file ;
    grid mesh ;
    triangle shape ;
};

int main()
{
    car ferrari( "cavallino_rampante.png" ) ;

    ferrari.mesh.print_file_name() ;
        ferrari.mesh.the_vertex.print_file_name() ;
            ferrari.mesh.the_vertex.one.print_file_name() ;
            ferrari.mesh.the_vertex.two.print_file_name() ;

    ferrari.shape.print_file_name() ;
        ferrari.shape.the_vertex.print_file_name() ;
            ferrari.shape.the_vertex.one.print_file_name() ;
            ferrari.shape.the_vertex.two.print_file_name() ;
}

void component::print_file_name() const
{
    std::cout << std::setw(20) << typeid(*this).name() << '@' << this << "::print_file_name - "
              << "car@" << std::addressof(main_object) << " : " << std::quoted( main_object.file_name() ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/2bb360db2bdfb3f7
http://rextester.com/BGE5962
Thanks for reply.

Actual implementation is done.

By that time we didn't refer main object in in all low level object because its not required.

Now requirement is changed i have refer it to call some function.

As you suggested passing main object in all constructors takes lot of changes is there any way to do this and i don't want maintain list of object and accessing it through index.

could u suggest me some good design
Last edited on
Without changing the constructors and without a lookup table? This is getting messy.

Here is a kludge which works if (and only if) the constructors of sub-objects under the main object do not cause another instance of the main object to be instantiated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <typeinfo>
#include <iomanip>
#include <stdexcept>

struct car ;
namespace detail { thread_local const car* current_car = nullptr ; }

struct component // life-time of a component does not exceed the life-time of the main car object
{
    virtual ~component() = default ;
    // defaulted copy/move constructor, copy/move assignment

    void print_file_name() const ;

    // ...
    const car* main_object = detail::current_car ;
};

struct some_sub_object : component { /* ... */ } ;

struct vertex : component { some_sub_object one ; some_sub_object two ; /* ... */ } ;

struct grid : component { vertex the_vertex ; /* ... */ } ;

struct triangle : component { vertex the_vertex ; /* ... */ } ;

struct car
{
    explicit car( const std::string& file_name ) : current_car_setter(this), path_to_file(file_name) {}

    const std::string& file_name() const { return path_to_file ; }

    struct current_car_setter_t { explicit current_car_setter_t( const car* c ) { detail::current_car = c ; } };
    current_car_setter_t current_car_setter ; // *** must be declared before any other non-static member object

    std::string path_to_file ;
    grid mesh ;
    triangle shape ;
};

int main()
{
    car ferrari( "cavallino_rampante.png" ) ;

    ferrari.mesh.print_file_name() ;
        ferrari.mesh.the_vertex.print_file_name() ;
            ferrari.mesh.the_vertex.one.print_file_name() ;
            ferrari.mesh.the_vertex.two.print_file_name() ;

    ferrari.shape.print_file_name() ;
        ferrari.shape.the_vertex.print_file_name() ;
            ferrari.shape.the_vertex.one.print_file_name() ;
            ferrari.shape.the_vertex.two.print_file_name() ;
}

void component::print_file_name() const
{
    if( main_object == nullptr ) throw std::logic_error( "no main object" ) ;

    std::cout << std::setw(20) << typeid(*this).name() << '@' << this << "::print_file_name - "
              << "car@" << main_object << " : "
              << std::quoted( main_object->file_name() ) << '\n' ;
}

http://coliru.stacked-crooked.com/a/f6fb6cab74de49d2
Thanks.
Topic archived. No new replies allowed.