Using shared_pointer in classes?

solved
Last edited on
The design problem that you are facing is not quite clear. Perhaps, you are looking for CRTP?
https://en.wikipedia.org/wiki/Curiously_recurring_template_pattern

Something along these lines?

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
#include <memory>

struct A
{
    using pointer = std::shared_ptr<A> ;

    A( int ii = 0 ) : i(ii) {}
    // ..
    int i ;
};

struct B
{
    using pointer = std::shared_ptr<B> ;
    // ..
};

template < typename DERIVED > struct C
{
    using pointer = std::shared_ptr<C> ;

    C( int ss = 0 ) : s(ss) {}
    virtual ~C() = default ;
    C( const C& ) = default ;
    C( C&& ) = default ;
    C& operator= ( const C& ) = default ;
    C& operator= ( C&& ) = default ;

    static C<DERIVED>::pointer fun( A::pointer pa1, A::pointer pa2 )
    { return std::make_shared<DERIVED>( pa1->i + pa2->i ) ; }

    virtual B::pointer test() = 0 ;

    // ...

    int s = 0 ;
};

struct func1 : C<func1>
{
    using C<func1>::C ;

    virtual B::pointer test() override { return std::make_shared<B>() ; }
};
I have no idea what you are trying to solve, what approach did you chose or what problem do you have implementing it.
Be a little more descriptive, we are not aware of your situation.

Also, learn terminology.


> a class that has a bunch of shared pointers like this
you haven't show any member variable.

> Inside Class A I have another shared_pointer typedef,
¿why is that relevant?

> I also need to pass two shared_pointers into this function
¿which function? `test()' does not take any parameters.
Also, ¿why is that a problem?

> and the typedef is also a shared function
typedef is not a function

> I'm confused on how to use the shared pointers or how to pass them into this
> new class
like you do in any other case.


Also, would recommend to use another name for your class, one that doesn't clash with the name of its functions.
Last edited on
solved
Last edited on
Again, ¿what problem are you trying to solve?


> class BothTrue : public Gates
`BothTrue is the name of an static member function. It may lead to confusion if you you are refering to the constructor or to the function.


1
2
3
4
    State out() const override
    {
        return out;
    }
¿how different would be the other overloads? ¿couldn't you move this to the parent?


1
2
3
4
BothTrue(InputPoint c, InputPoint d)
    {
      //How can I convert the inputs above to take them as arguments below?
        if(c==true && d==true)
Not sure what you are asking here.
you may use `c' and `d' inside that constructor as you like. If you want to compare c against true, then no idea, I do not know what that class does, what is its purpose or how is it made (perhaps c.numberpriv == State::On)
Last edited on
solved
Last edited on
if( c->number()==State::On and d->number()==State::On )


> class BothTrue is a subclass of class Gate.
and function BothTrue is an static member function of class Gate.
¿have you implemented that?

looking here http://www.cplusplus.com/forum/beginner/179691/
I'm trying to make a pure virutal method. So I added virtual class 1 function(); to the bottom of my third class, and I made all the members static.
¿why did you "made all the members static"?
¿do you know what static means?
Last edited on
solved
Last edited on
Let me see if I understand, you are trying to simulate a logic circuit.

1
2
3
4
5
6
class Gates
{
	public:
		virtual State out() const = 0;
		virtual ~Gates() = default; //needed because there is a virtual member function
};
that's the whole `Gates' class.
Note that `Gates' it is not aware of any subclasses that it may have, adding new subclasses require no modification to `Gates'.
The only operation that can perform on a gate is to query its output, that function would be overloaded on the derived classes.

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
class BothTrue /*aka And*/: public Gates{
public:
	BothTrue(Input a, Input b); //a binary gate, it has two inputs
	State out() const override{
		return
			a->number() == State::On //number() seems to be a bad name
			and b->number() == State::On;
	}
private:
	Input a, b; //it needs to store those inputs to be able to operate on them later
};

BothTrue::BothTrue(Input a, Input b): //constructor
	a(a), //initialization list
	b(b)
{}


//another subclass
class Anyone /*aka Or*/: public Gates{
public:
	Anyone(Input a, Input b); //again, a binary gate
	State out() const override{
		return
			a->number() == State::On
			or b->number() == State::On;
	}

private:
	Input a, b;
}

//yet another one
class Opposite /*aka Not*/: public Gates{
public:
	Opposite(Input a); //unary gate
	State out() const override{
		return
			a->number() == State::Off;
	}
private:
	Input a;
}

//one more
class JustOne /*aka Xor*/: public Gates{
public:
	JustOne(Input a, Input b):
	//just pseudo, you'll need std::make_shared
		result(
			Input(
				BothTrue( //~AB
					Input(Opposite(a)),
					b
				),
			),
			Input(
				BothTrue( //A~B
					a,
					Input(Opposite(b)
				)
			)
		)
	{}

	State out() const override{
		return result->out();
	}
		
private:
	Anyone result; //would implement ~AB + A~B
}
last one is only to show composition, but the result was hideous.


Edit: I forgot about the typedef, it should be `InputPoint' instead of just `Input' in the constructor and member variables.
Last edited on
solved
Last edited on
I don't see why it may give you that error message.
`.number()' returns a State and State::On is a State, ¿right?

You shouldn't cast away your error, ¿can upload your project to github or similar so we can take a look at it?
solved
Last edited on
The error was on the return. `.out()' should return a `State' but was trying to return a bool.
1
2
3
4
5
//for Opposite
if(a->number() == State::Off)
   return State::On;
else
   return State::Off;
I'll recommend you to make a function to convert from bool to State

Here's a sample https://github.com/ne555/gates
Thanks, ne555!
Last edited on
IMO, you want to shoot an ant that it's standing on your foot.
`BothTrue' already has a meaning as a class, then you want to use a function with a suspicious similar name to return a pointer to that same class.
1
2
BothTrue(a, b); //an object
Gate::BothTrue(a, b); //a pointer 



If you do it as an static member function, the problem is that objects of that class may call the method
1
2
Anyone foo(a, b);
foo.BothTrue(a,b); //¿? 



If you really want to do it, perhaps a namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//gates_hidden.h
/*this classes are not mean to be used directly*/
typedef std::shared_ptr<Gate_> GatePoint;
class Gate_{ //note the underscore
   //...
};
class BothTrue_: public Gate_{
   //...
};
//...


//gates.h
#include "gates_hidden.h"
namespace Gate{
   inline GatePoint BothTrue(InputPoint a, InputPoint b){
      return std::make_shared<BothTrue_>(a, b);
   }
}
Then if you later want to add another gate kind
1
2
3
4
5
6
7
8
9
class Foo_: public Gate_{ //again, not used directly
   //...
};

namespace Gate{
   inline GatePoint Foo(InputPoint a, InputPoint b, InputPoint c){
      return std::make_shared<Foo>(a, b, c);
   }
}


didn't put much thought on it, there may be a better way.
Topic archived. No new replies allowed.