higher class, lower class <vector>

hi,
i ve following example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class a{
	public:
	void set_value(int i){v = i;}
	private:
	int v;
}

class b{
	
	public:
		bool set_value(unsigned int index, int i){if((values.size() >= index)return false; values.at(index).set_value(i);}
		void add_a(a new_obj){values.push_back(new_obj);}
	private:
	std::vector<a> values;
}

void main(void)
{
	a a_test;
	b b_test;
	b_test.add_a(a_test);
	b_test.set_value(5, 100); // no crash return value false
}


is there an option/possibility to use the check and add all functions from a without an extra class b. every function from class a, needs to be defined in b, this is cumbersome.


thanks for the help
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
#include <iostream>
#include <vector>

class a{
    public:
        void set_value(int i){v = i;}
    private:
        int v;
}; // error: expected ; after class, corrected, added ;

class b{

    public:
        // error: if statement has empty body; corrected, remove spurious (
        // warning: control may reach end of non-void function; corrected. added return true
        // logical error: corrected, changed >= to <=
        bool set_value(unsigned int index, int i){if(/*(*/values.size() /*>=*/ <= index)return false; values.at(index).set_value(i); return true ; }
        void add_a(a new_obj){values.push_back(new_obj);}
    private:
        std::vector<a> values;
}; // error: expected ; after class, corrected, added ;

/*void*/ int main(void) // error: main must return int; corrected, changed to int
{
    a a_test;
    b b_test;
    b_test.add_a(a_test);
    const bool result = b_test.set_value(5, 100); // no crash return value false
    std::cout << std::boolalpha << result << '\n' ; // false
}
hi JLBorges,
thanks for the code correction, now it executes in the c++ shell without an error.

but is there an solution for the problem?

i dont want to add every function that is in class a available, to class b.


thanks
You don't want to add wrappers to b.
You can bypass b:
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
#include <iostream>
#include <vector>

class a{
    public:
        void set_value(int i){v = i;}
    private:
        int v {};
};

class b{
    public:
        void add_a(a new_obj){ values.push_back(new_obj); }
        const std::vector<a>& guts() const { return values; }
    private:
        std::vector<a> values;
};

int main(void)
{
    a a_test;
    b b_test;
    b_test.add_a(a_test);
    b_test.add_a(a_test);
    auto foo = b_test.guts();

    auto index = 5;
    if ( index < foo.size() ) foo.at(index).set_value(100);

    std::cout << foo.size() << '\n';
}

However, then your "if valid index, then ..." logic has to be elsewhere.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <functional>
class b{
public:
	template <class F, class... Args>
	bool exec(size_t index, F function, Args... args){
		if(index < values.size()){
			std::bind(function, &values[index], args...)();
			return true;
		}
		return false;
	}

	void add_a(a new_obj) {
		values.push_back(new_obj);
	}
private:
	std::vector<a> values;
};

int main(){
	//...
	b_test.exec(0, &a::set_value, 42); //true
	b_test.exec(5, &a::set_value, 100); //false
}

Topic archived. No new replies allowed.