This question already has answers here:
Can I avoid an ambiguity, when I declare a fixed length vector in class?
(2 answers)
Closed 8 years ago.
Can someone explain me why I can make a vector in the int main(), like this:
std:: vector<double> a(20);
but I cannot use this while i am creating a c++ class:
class A
{
std:: vector<double> a(20);
}
You need to initialize the vector in A's constructor:
#include <vector>
class A
{
std::vector<double> a;
public:
A() : a(20) {}
};
You need a constructor that initializes the vector in order to get the behavior you want. (Also I see some syntax mistakes that need to be corrected in the question.) The class will end up looking something like this:
#include<vector>
class A{
public://make the constructor public
A():a(20)//initialize the vector in the constructors initialization list
{}
std::vector<double> a; //can be public or private depending on your use case
}; //don't forget the trailing semicolon!!!
Related
This question already has answers here:
What are the rules for calling the base class constructor?
(10 answers)
Closed 1 year ago.
#include <iostream>
using namespace std;
class A{
public:
A(){
cout << "Hello World";
}
};
class B:public A{
public:
B(){
cout << "World";
}
};
int main(){
B obj1;
return 0;
}
Why does this program print Hello WorldWorld, shouldn't it print World because I have created object of class B, so why is the constructor of A being called?
Conceptually, a base class becomes an unnamed sub object in the derived class, whose members are available in the same scope without extra qualification, and must be initialized.
You cannot avoid that initialization - which means a relevant constructor will be called or if it cannot be constructed then compiler will not allow you to inherit.
What you probably mean is whether the constructor should have overridden the base version, the simple answer is it cannot. If you want that effect - which will not work in constructors in a common sense way - you need to use virtual functions and overriding.
This question already has answers here:
automatically convert list of pointers to derived class to list of pointers to base class
(4 answers)
Closed 4 years ago.
I would like to know why the compiler doesn't allow the second use of "print_all" function.
I have to give an example of a bad thing that could happen if the compiler would allow it.
#include <iostream>
#include <list>
using std::list;
class foo {
class bar : public foo {
static void print_all(list<foo *> &L) {
list<foo *> LF;
list<bar *> LB;
print_all(LF); // works fine
print_all(LB); // static semantic error
}
};
};
list<foo *> is an unrelated type to list<bar *>. The function is specified to accept one, but not the other.
But class bar inherits from class foo
That is irrelevant, because the argument of your function isn't foo&. What's relevant is whether list<bar *> inherits list<foo *>. It doesn't. std::list does not have a base class.
This question already has answers here:
Why don't std::vector's elements need a default constructor?
(4 answers)
Closed 4 years ago.
I read that if I want to define array of elements from a type T , so class T must a default c'tor (It's correct, yes?).
But what about defining container (from the STL) of elements from type T?
You don't need default constructors in both cases.
#include <iostream>
#include <vector>
struct A
{
int x;
A(int x) : x(x) {}
};
int main()
{
A arr[3] {A(0), A(1), A(2)}; // An array
std::vector<A> vec; // A std container
vec.push_back(A(0));
vec.push_back(A(1));
vec.push_back(A(2));
}
Though, for some containers the lack of default constructor disables some operations, like operator[] of std::map and std::unordered_map.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Initializing in constructors, best practice?
I'm new to C++.
Suppose we have this class definition:
Class MyClass {
int a;
int b;
//....
}
I would like to know which is the difference between the two class contructors:
public:
MyClass(int a, int b) : a(a), b(b) {}
and (i would say Java-style):
MyClass(int a, int b) {
this->a = a;
this->b = b;
}
I suppose the first is better in C++; right? why?
The first one (using initializer list) initializes the data members to the given values. The second one initializes them first, then assigns them the values. That is the reason the first is prefered. There is no unnecessary assignment operation there.
This is particularly important when your data members are expensive to construct and/or assign to. Also bear in mind that some types are not default constructable, making it mandatory to use the initializer list.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does a colon following a C++ constructor name do?
Class construction with initial values
I saw code that looked like this:
class Demo
{
Joystick joystick;
public:
Demo(void):
joystick(1) // The first USB port
{
/* snip */
}
};
Joystick is being initialized before the bracket in the constructor. What does it mean when you do that? What is this called? I'm assuming it differs in some way then initializing joystick inside the bracket -- in what ways does it differ?
It is called an initializer list, and it does differ from initializing inside the body of the constructor.
You can call the constructors of every data member in the class in the initializer list. Also you can call a custom parent class(s) constructor within it, if you didn't, every data member or parent class you don't initialize with the initializer list will be initialized with its default constructor, if it doesn't have, you will see a compiler error.
This is an extended example:
class Parent
{
bool b;
public:
Parent(bool B): b(B)
{
}
};
class Child: public Parent
{
int i;
double d;
public:
Child(int I, double D, bool B): i(I), d(D), Parent(B)
{
}
};
For the order they are called, see this question and this question.
In fact explaining it is an entire article as it's a basic and important thing in classes, just try Googling it and reading some results.