Initialization by constructor in c++ - c++

I have question about constructor,
why following code works correctly:
#include <iostream>
using namespace std;
class mycl
{
private:
int a;
//struct
//{
char b,c;
//} ms;
public:
mycl (int _a,char _b,char _c):a (_a), b (_b), c (_c){}
};
int main() {
// your code goes here
mycl slc (15, 'a', 'f');
return 0;
}
https://ideone.com/wBgM1b
but there is a compilation error in this one
https://ideone.com/Yqxvzk
is it possible to initialize members of complex types this way?
p.s.
thank for translate and for answer.
sorry about wrong language

You want:
mycl(int _a, char _b, char _c) : a(_a), ms{_b, _c} {}
// ^^^^^^^^^^

Related

What syntax should i use for member initializers?

What syntax should i use for member initializers in c++?
I know how to use it for Attribute but not for functions.
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int a, int b,int c):
regVar(a), constVar(b),function(c)
{}
public:
int regVar;
const int constVar;
void function(int a){
cout << a;
}
};
int main(){
MyClass myclass(10,20);
cout << myclass.regVar;
}
Thanks in advance
I tried function(c), c.function etc.

Problems with copy constructor and vector

I met a problem with copy constructor and vector.
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
int main(int argc, char const *argv[])
{
A aa;
A a2 = aa;//it works well
//addA(va, aa);
return 0;
}
But when I do something like this:
#include <iostream>
#include <vector>
using namespace std;
class B {
private:
int val;
};
class A {
private:
B b;
public:
A() {}
A(A& a) {}
};
void addA(std::vector<A>& va, A a) {
va.push_back(a);
}
int main(int argc, char const *argv[])
{
std::vector<A> va;
A aa;
A a2 = aa;
addA(va, aa); // I got a compile error
return 0;
}
there is a compiler error.
But if I change the args in class A's copy constructor to const A&, it works well.
Why is this so?
The argument to std::vector<A>::push_back is const A &, so when the vector tries to copy that into the storage it allocated for the element, it requires a constructor that takes const A &. Because yours doesn't, it can't be used.

About pointer to member function of derived class

Here's my code, and the IDE is DEV C++11
#include<iostream>
using namespace std;
class A{
public:
int a=15;
};
class B:public A
{
};
int main(){
int A::*ptr=&B::a; //OK
int B::*ptr1=&A::a; //why?
int B::A::*ptr2=&B::a;//why?
int B::A::*ptr3=&A::a; //why?
}
I have read Programming Languages — C++ and I know the type of &B::a is int A::*, but I don't realise why the next three lines will pass the compilation.
And the weirdest thing to me is the syntax of int B::A::* , what's the meaning of this? I'm just a newcomer of C/C++, so please tolerate my weird question.
Diagram representation may help you understand why it is ok and compiles
Interesting will be once you reinitialize the same variable in inherited class
#include<iostream>
using namespace std;
class A {
public:
int a = 15;
};
class B :public A
{
public:
int a = 10;
};
int main() {
int A::*ptr = &B::a; //Waring class B a value of type int B::* cannot be
//used to initialize an entity of type 'int A::*'
int B::*ptr1 = &A::a; // why?
int B::A::*ptr2 = &B::a;//Waring class B a value of type int B::* cannot
// be used to initialize an entity of type 'int A::*'
int B::A::*ptr3 = &A::a; //why?
}

create an unary_function functor for non-static member function

The code should explain my difficulty. Though the code itself is quite meaningless, I'm planning to add containers in MyClass, and use algorithms with member functions.
#include <cstdlib>
#include <algorithm>
#include <functional>
using namespace std;
class MyClass
{
public:
MyClass() { a = 0; }
~MyClass() {}
private:
int a;
bool tiny_test (int);
int Func();
};
bool MyClass::tiny_test (int b)
{
return a == b;
}
int MyClass::Func()
{
// does not compile
(mem_fun(&MyClass::tiny_test))(this);
// commented below is another attempt, also no success
//mem_fun1_t<bool, MyClass, int> tmp_functor = mem_fun(&MyClass::tiny_test);
//tmp_functor(this);
return 0;
}
int main(int argc, char** argv)
{
return 0;
}
Thanks a lot! Btw, I'm not using a static member function, simply because I believe it must work for non-static member functions.
P.S. Eric, Jarod42, thanks for prompt replies!
bool MyClass::tiny_test (int b)
{ // ^^^^^ You missed this argument
return a == b;
}
Try this:
// Supply one more argument. E.g., 3
(mem_fun(&MyClass::tiny_test))(this, 3);

'undeclared identifier" while trying to make an instance in c++

this is my code:
#include "stdafx.h"
#include <iostream>
int main()
{
Box *b = new Box(1,2,3);
}
class Box
{
private:
int a;
int b;
int c;
public:
Box (int aa, int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
};
it doesn't compile.
(i did not split it to an h file and a cpp file)
thanks in advance
Put main after your class
class Box
{
private:
int a;
int b;
int c;
public:
Box (int aa, int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
};
int main()
{
Box *b = new Box(1,2,3);
}
The forward declaration class Box; at top in this case won't work, however it will work if you only use Box* b;
You need to declare your class above the code that uses it.