I know you can't usually define things in the class like this:

class MyClass
{
  double DefaultForward[3] = {0.0, 1.0, 0.0};
};

but I have some values that don't really make sense to set in the constructor - they are more "deeply connected" as THIS SHOULD ALWAYS BE THIS VALUE!! Like a define, but I don't think you can declare arrays in a define?

Is there some magic keyword that lets you do something like this - something like const static double Default.... Thanks,

Dave

Recommended Answers

All 5 Replies

you can place your array into private, and just initialize values in constructor
Example

class MyClass
{
public:
    MyClass()
    {
          DefaultForward[0] = 0.0;
          DefaultForward[1] = 1.0;
          DefaultForward[2] = 0.0;
    }
private:
          double DefaultForward[3];
}

Ok, I was hoping for something better than initializing it in the constructor - does anyone else have a different way?

Thanks,

Dave

http://groups.google.com/group/comp.lang.c++/browse_thread/thread/25bc613dfbf72dc2/be4b67885161b012?ie=UTF-8&q=group:comp.lang.c%2B%2B+const+class+member+constructor+array#be4b67885161b012

[edit]

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

class T
{
   const std::vector<double> DefaultForward;
public:
   T(const std::vector<double> &dfinit) : DefaultForward(dfinit)
   {
   }
   friend std::ostream& operator<< (std::ostream &o, const T& t)
   {
      std::copy(t.DefaultForward.begin(), t.DefaultForward.end(),
                std::ostream_iterator<double> (o, ","));
      return o;
   }
};


int main()
{
   const double init[3] = {0.0,1.0,0.0};
   const std::vector<double> default_forward(init, init + sizeof init / sizeof *init);
   T t(default_forward);
   std::cout << t << '\n';
   return 0;
}

?

It's not sure, but try use following constrction

class CDoubleArray
{
public:
	CDoubleArray()
	{
		a[0] = 0.0;
		a[1] = 1.0;
		a[2] = 0.0;
	}
	double a[3];
	double &operator[](int index)
	{
		return a[index]; //need to check index out range
	}
};

class MyClass
{
public:
	MyClass()
	{
	}
	CDoubleArray DefaultForward;
};

and then

MyClass *c = new MyClass();
	std::cout << c->DefaultForward[0] << ' ' << c->DefaultForward[1] << ' ' << c->DefaultForward[2] << ' ' << std::endl;

just for check

P.S. but it's still constructor intialized...

Here, I think this is what I was looking for:

#ifndef POINT_H
#define POINT_H

class Point
{

	public:
		Point(){}
		Point(const double x, const double y, const double z) : X(x), Y(y), Z(z) {}
		
		static const double Origin[3];
		
	private:
		double X,Y,Z;
		
		
};

const double Point::Origin[3] = {1.0, 2.0, 3.0};

#endif
#include <iostream>

#include "Point.h"

int main(int argc, char* argv[])
{
	Point P;
	std::cout << P.Origin[0] << " " << P.Origin[1] << " " << P.Origin[2] << std::endl;
	return 0;
}

Thanks all - it was in the link you posted Protuberance.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.