Is it possible to implement a graph with object of a class?
I mean, I made a class and it has many inherited classes, that have 2 pointers and another value. These 2 pointers have to point to the values of two different object of inherited classes.
If I don't explain well, I can try to be more precise..
I'm not entirely sure what you're asking. Could you post a representative example of what you have?
To represent a general graph you need an object that is capable of pointing to an arbitrary number of other objects. A common way of doing this is by storing pointers in a vector. For example,
I have to make a class Circuit, composed by logic port of a class called Port. Each element of Port has two pointers (I made pointers to bool but I don't know if it's better to point to other element of class Port) and a value that can be 0,1 or a character.
Yes, but I have two bool pointers..
I don't know which is better, because first elements have to point to bool values, given in an input vector, then other elements can point to other ElectronicComponent.
You need to determine if your circuit can have feedback. In other words, can there be a cycle in the circuit, such as with a flip-flop or amplifier, or are you guaranteed that the output of each component can never affect that component's input?
If the circuit can contain cycles then you need to add some logic to prevent recursion in your get_state() method.
You need to determine if your circuit can have feedback. In other words, can there be a cycle in the circuit, such as with a flip-flop or amplifier, or are you guaranteed that the output of each component can never affect that component's input?
If the circuit can contain cycles then you need to add some logic to prevent recursion in your get_state() method.
In this case, you will almost certainly also need an update_state() function. update_state() will set the state of the component by calling first and second's get_state() and simulating the behavior of the component. For example, AND would do state = first->get_state() && second->get_state(). Meanwhile, get_state() will simply return the value of state.
This would not only solve the problem of infinite recursion, but it would also simulate finite-speed propagation of signals inside the circuit, without which it's impossible to correctly simulate circuits such as flip-flops.
I have to check if there is a feedback, bacause I have to make (also) only combinational circuits.
But sorry, I don't understand what are you saying about this function
No, you're passing a reference to a myClass object, not a pointer:
1 2
void setInput(const myClass &a); // a is a reference to a myClass object
void setInput(const myClasss *a); // a is a pointer to a myClass object
Don't think if references and pointers as the same thing. Think of a reference as a synonym for another object. Since it's an object and not a pointer, you need to set _x1 to it's address:
1 2 3
void setInput(const myClass& a){
_x1 = &a;
}
I know that the syntax is confusing. Sometimes & means "reference to" and sometimes it means "address of" and sometimes it even means "bit-wise and".