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.
> 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)
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.
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.
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.
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.