How to implement c++ template<T> in golang [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How to translate this cpp code to golang ?
template<T> class CppTemp {
T a;
T* pa;
T foo(T &t);
};
template<T> T foo2(const T &t)

Go doesn't support templates or generics. There are three things you can do:
use non-empty interfaces where applicable
generate code with go generate
use interface{}:
type GoTemp struct {
a interface{}
}
func (gt *GoTemp) foo(v interface{}) {
// ...
}
func foo2(v interface{}) {
// ...
}

Related

Move unique_ptr ownership from one class to another [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I want to create a unique pointer in one class, class A, then pass on the ownership to another class, class B. Am I ok doing such a thing?
The code below gives me error in getC:
error: call to deleted constructor of 'std::unique_ptr<C>
What am I doing wrong?
class A {
...
void func(shared_ptr<B> Bptr) {
A_pass = make_unique<C>();
Bptr->setPass(move(A_pass));
}
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
class B {
...
void setPass(unique_ptr<C> pass_ptr){
B_pass = move(pass_ptr);
}
unique_ptr<C> B_pass;
}
edit: update the question
Your question does not state where the compiler error occurs, but I would guess it’s the getC() member function:
class A {
...
unique_ptr<C> getC()
{
return A_pass;
}
unique_ptr<C> A_pass;
};
The function as written is attempting to copy A_pass, which of course is not possible for the std::unique_ptr<T> type.
You can rewrite it to explicitly move from the source (I’m not able to test this):
unique_ptr<C> A::getC() {
return A_pass.release();
// alternative: return unique_ptr<C>(std::move(A_pass));
}

how to declare a generic class in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
My main question is: can I put the generic type before a class definition?
Like, can I do something like this:
generic class Classname (ParameterType parameter)
{
cout << "Hello world";
}
Templates are the way to do generics in C++. You can write it like this:
template<class ItemType> class ClassName
{
public:
ClassName(const ItemType& newdata) : data(newdata) {}
private:
ItemType data;
};
Later on in main:
ClassName<int> data1(1);
ClassName<char> data2('A');

C++ When comparing, pass class as pointer or normally? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm quite new to C++, and since I want my code to be good, I have a small question, let's say I have a function
UI::GetColor(CClass Class)
{
if (Class.m_Something)
return 0;
return 1;
}
Is it better to pass the CClass as a pointer, so it's not being copied or not? I saw a lot of code using different styles of that and I'm kind of confused which one is better and why. Thanks for answers.
The function should be written as follows:
class UI {
...
int GetColor(const CClass &c) const {
if (c.m_Something) {
return 0;
}
return 1;
}
}
The reference avoids an unnecessary copy; the const-parameter states that the parameter will not be changed; the const-function declarator states that the function will not change the this-object (i.e. the UI-instance on which it is called).

How to override the setter? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an private property in my class and I want to add some code on its setter, how can I accomplish that?
I might do that using method but I don't want to just to avoid using property.
private:
float damage;
You can replace your plain old data type with a class type to do what you want:
class Damage
{
public:
Damage& operator=(float x) { value = x; /* add code here*/ }
operator float() const { return value; }
private:
float value;
};
Then just replace your float with Damage and you can make it behave how you want.

Should we do unit testing for default methods in interfaces (Java 8)? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I feel a bit confused about the default methods implementations in interfaces introduced in Java 8. I was wondering if we should write JUnit tests specifically for an interface and its implemented methods. I tried to google it, but I couldn't find some guidelines. Please advise.
Its depends on the method complexity. It is not really necessary if the code is trivial, for example:
public interface MyInterface {
ObjectProperty<String> ageProperty();
default String getAge() {
return ageProperty().getValue();
}
}
If the code is more complex, then you should write a unit test. For example, this default method from Comparator:
public interface Comparator<T> {
...
default Comparator<T> thenComparing(Comparator<? super T> other) {
Objects.requireNonNull(other);
return (Comparator<T> & Serializable) (c1, c2) -> {
int res = compare(c1, c2);
return (res != 0) ? res : other.compare(c1, c2);
};
}
...
}
How to test it?
Testing a default method from an interface is the same as testing an abstract class.
This has already been answered.