Allocating an array of C++ objects [duplicate] - c++

This question already has answers here:
Object array initialization without default constructor
(14 answers)
Closed 8 years ago.
I want to allocate an array of C++ objects using the following code:
class myClass {
public:
myClass(int userValue)
: value(userValue)
{ }
}
private:
int value;
};
int main(){
myClass* objArray = new myClass(22)[5];
return 0;
}
But it gives me the following error:
In constructor ‘myClass::myClass(int32)’:
error: expected ‘;’ before ‘[’ token
objArray = new objArray(22)[5];
How should I create an array of objects then while passing parameters to them?

Use std::vector.
std::vector<myClass> objArray(5, 22);

Related

Why does calling a functor with an undeclared variable work? [duplicate]

This question already has answers here:
Why does C++ allow us to surround the variable name in parentheses when declaring a variable?
(2 answers)
Closed 5 years ago.
class foo {
public:
bool operator () (int & i) {
return true;
}
};
int main() {
foo(WhyDoesThisCompile);
return 0;
}
When passing WhyDoesThisCompile (without spaces) to the functor, the program compiles.
Why is this? I tested it on clang 4.0.0.
You are not invoking the functor.
You are declaring a foo, called WhyDoesThisCompile.
Yes, despite the parentheses.
I guess you meant this:
foo()(WhyDoesThisCompile);
// ^^^^^
// temp ^^^^^^^^^^^^^^^^^^^^
// of invocation of op()
// type
// `foo`
… which doesn't.

Pass 2D array of pointers by reference to function [duplicate]

This question already has answers here:
How do I pass a reference to a two-dimensional array to a function?
(5 answers)
Reference to a Two-Dimesional Array
(2 answers)
Closed 8 years ago.
I have a problem. I dont know how to pass 2D array of pointers to fuction by reference.
class SomeClass
{
//body of class
}
void somefunction(SomeClass ***array)
{
//body of function
}
int main()
{
SomeClass * array[10][10]
someFunction(array?????)
}
Anyone know how to pass this array by reference??
Literally
void somefunction(SomeClass *(&array)[10][10])
{
}
int main()
{
SomeClass *array[10][10];
someFunction(array);
}

Evaluating C++ pointer to function expression [duplicate]

This question already has answers here:
How to call through a member function pointer?
(2 answers)
Closed 8 years ago.
I have a C++ class which contains the following definition:
class SomeClass:
public BaseClass
{
public:
SomeClass();
bool SomeClass::MyFunc( Json::Value& jsonRoot)
typedef bool(SomeClass::*PFUNC)(Json::Value&);
std::map<std::string, PFUNC> m_map;
}
later on in the c++ code i add values to the map variable using the following line:
SomeClass::SomeClass()
{
m_map["func"] = &SomeClass::MyFunc;
}
and for execution within one of SomeClass's methods:
std::map<std::string, PFUNC>::iterator itFunction = m_map.find("func");
if (itFunction != m_map.end())
{
PFUNC pfParse = m_map["func"];
Json::Value x;
this->*pfParse(x);
}
I end up getting the following compilation error:
error C2064: term does not evaluate to a function taking 1 arguments
I even tried using the iterator explicitly - this->*iterator->second(...) but ended up with the same error.
what am i doing wrong?
thanks
() has higher precedence than ->* so pfParse(x) is evaluated first. You need to use parenthesis to sequence the evaluation:
(this->*pfParse)(x);

Is it Ok to call method on NULL pointer instance of a class? [duplicate]

This question already has answers here:
Accessing static member through invalid pointer: guaranteed to "work"? [duplicate]
(3 answers)
Closed 8 years ago.
This C++ code works for my platform and compiler (Windows, GCC 4.7):
#include <stdio.h>
class A {
public:
/* ... */
int size() const
{
if ( this == NULL ) {
return 0;
}
return m_size;
}
private:
int m_size;
};
int main()
{
A* a = NULL;
printf( "%d\n", a->size() );
}
But is this code valid standard C++ and portable?
Is it Ok for method to accept this == NULL?
No, that's not OK. Any dereference of a NULL pointer is undefined behavior. It happens to work because it's not a virtual function (therefore there's no jump through the object's vtable), but that's not an excuse for doing what you're doing.

initializing an arbitrary-size array of pointers in a constructor [duplicate]

This question already has answers here:
How to make an array with a dynamic size? General usage of dynamic arrays (maybe pointers too)? [closed]
(5 answers)
Closed 8 years ago.
In a project that I'm working on, I need to make a class that contains an array of pointers to other objects of the same class. I'm currently having trouble initializing this array.
Example:
class MrClass{
MrClass* otherInstances[];
public:
MrClass(MrClass* x[]){
otherInstances = x;
}
}
This array must be arbitrarily sized, since the number of instanced of the class to be passed is defined at compile time and it must be of pointers because multiple instances of the class must have access to the same objects.
Correct solution
Use std::vector<MrClass *> or std::array<MrClass *>. Or even better, std::vector<std::shared_ptr<MrClass>>
class MrClass{
std::vector<std::shared_ptr<MrClass>> otherInstances;
public:
MrClass(std::vector<std::shared_ptr<MrClass>> const & x)
: otherInstances(x)
{
}
}
Auxiliary solution
If you really need an array (and you really know, what you're doing), do the following:
class MrClass{
MrClass ** otherInstances;
int otherInstancesCount;
public:
MrClass(MrClass ** x, int count){
otherInstances = x;
otherInstancesCount = count;
}
}
Arbitrarily sized arrays are written std::vector in C++. So
you'd have:
class MrClass
{
std::vector<MrClass*> otherInstances;
public:
MrClass( std::vector<MrClass*> const& initialValues )
: otherInstances( initialValues )
{
}
};