How to pass private class member by constant reference? [closed] - c++

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
This is my class
#include <vector>
class MyClass
{
public:
void print(); // prints X to console
private:
std::vector<int> X (4, 100);
};
When I call the print() method I want X to be passed by constant reference to print(). How can I do that? Currently is looks like as X is passed by value.

Ok, maybe this will help you to start thinking in c++, even if you did not make clear explanation what you need, if I may guess, what you probably think of is that you want to be able to call print() member function on const instances of MyClass, or rvalue has been bound to const lvalue reference, so you need to mark print as const member function of type MyClass.
class MyClass
{
public:
void print() const; // prints X to console
private:
std::vector<int> X (4, 100);
};

Related

qt class using new return non-pointer [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 1 year ago.
Improve this question
Seeing that QT has a piece of code like this, how can new return an object instead of a pointer?
You made the wrong conclusion. new always returns a pointer. You need to read the documentation of the classes you are using to know how their constructor works. One way to enable foo f = new foo; is the following:
struct foo {
foo(foo*){}
foo(){}
};
int main(){
foo f = new foo;
}
Note that = here is not assignment! The object f is initialized by calling its constructor taking a pointer to a foo.

C++ difference between : , :: operators [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 6 years ago.
Improve this question
I new to programming. In stack overflow i couldn't see difference between : & :: is mentioned. Could anyone can explain in detail it helps to beginner learners like me. Thank you.
So you would use :: when you're defining/using methods from a class, so like for example
class foo{
public:
int bar;
int hi(int x);
int func(); // static member function
Foo(int num): bar(num) {}; // use of a colon, initialization list
};
int foo::hi(int x){
//define the function
}
Also if you have static member functions, you can just call those whenever through using foo::func(). You can find more about static member functions online.
The single colon is for member initialization list (you can look this topic up online) where you can initialization member variables in the construction of your class.
You can also find single colon used in polymorphism, when you derive a class from a base class. You can find more information about c++ polymorphism online.

Using generic template member in a class 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 8 years ago.
Improve this question
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.

c++ - How do you get a "pointer to self" from within a class? [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
In a method of an object of type class A, I handle an object of class B that has a public method .join(*A).
I want my object of type A calling this someObjectOfTypeB.join(*A) method, to use a pointer to itself as the parameter.
void A::someMethod()
{
B b();
b.join(I want to a to use a pointer to itself as a parameter);
}
A a();
a.someMethod();
Upon further investigation, this was not the problem as I led myself to believe; and is indeed the correct way of doing what I wanted to do.
Try using this:
void A::someMethod()
{
B b;
b.join(this);
}
As #AndrewLazarus and #JonathanWakely commented, use B b; instead of B b(). The later declares a function b without parameters which returns B, and that is not what you want.

Difference between function and functor [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 9 years ago.
Improve this question
I'm trying to write the following code:
class MyClass{
public:
virtual int operator()()=0;
}
int bar()
{
return 1;
}
int _tmain(int argc, _TCHAR* argv[])
{
class : public MyClass{
int operator()(){
return 1;
}
} foo;
}
What's difference between foo and bar?
bar is a function, but foo is a function object. So if (for example) you'll write something like:
std::for_each(myContainer.begin(), myContainer.end(), foo);
(see this)
then your overloaded MyClass::operator() will be called for every single object in your container. The main advantage of function objects in comparison with function pointers is that they can be nested, it means that you can define your function class anywhere, including another classes or even methods. Moreover, it is ideal for usage in template functions, because Function can be template type.