constructor

S4 constructor initialize and prototype

二次信任 提交于 2019-12-25 14:11:31
问题 I am trying to build a S4 object by calling the validity method in the constructor. setClass("Person", slot = c(Age = "numeric")) validityPerson<-function(object){ if(object@Age < 0)return("Age cannot be negative") TRUE } setValidity("Person", validityPerson) setMethod("initialize","Person", function(.Object,...){ validObject(.Object) .Object }) This code is problematic because I get new("Person", Age = 12) #Error in if (object@Age < 0) return("Age cannot be negative") : #argument is of

Template class constructor issues - designing a container for multidim arrays

▼魔方 西西 提交于 2019-12-25 11:56:36
问题 I'm trying to create my own container for an array of any dimension for numerical computing. I would like to do this using templates so that I could overload the subscript operator [] so that it works like normal arrays and vectors e.g. access entries like a[10][10][10] etc. I am having trouble getting the constructor to work when trying to create containers to hold multidimensional arrays. Please help! #include <iostream> #include <iterator> #include <algorithm> #include <vector> using

Template class constructor issues - designing a container for multidim arrays

谁说我不能喝 提交于 2019-12-25 11:56:31
问题 I'm trying to create my own container for an array of any dimension for numerical computing. I would like to do this using templates so that I could overload the subscript operator [] so that it works like normal arrays and vectors e.g. access entries like a[10][10][10] etc. I am having trouble getting the constructor to work when trying to create containers to hold multidimensional arrays. Please help! #include <iostream> #include <iterator> #include <algorithm> #include <vector> using

Overriding function called by base class?

喜欢而已 提交于 2019-12-25 09:46:56
问题 I have a class that is designed to be general-purpose, used wherever, that looks kinda like this: class FixedByteStream { public: FixedByteStream(const char* source) { size = strlen(source); copy(source); } /* Many more constructors here */ protected: void copy(const char* source) { address = allocate(); //... } /* Plus other functions that call allocate() */ char* FixedByteStream::allocate() { return (char*)malloc(size); } } I have then extended this class, so that it can use a project

Overriding function called by base class?

99封情书 提交于 2019-12-25 09:46:47
问题 I have a class that is designed to be general-purpose, used wherever, that looks kinda like this: class FixedByteStream { public: FixedByteStream(const char* source) { size = strlen(source); copy(source); } /* Many more constructors here */ protected: void copy(const char* source) { address = allocate(); //... } /* Plus other functions that call allocate() */ char* FixedByteStream::allocate() { return (char*)malloc(size); } } I have then extended this class, so that it can use a project

copy constructor is not called?

↘锁芯ラ 提交于 2019-12-25 09:29:42
问题 class X { int i; public: X(int m) : i(m) {}; X(const X& x) { //cout "copy constructor is called\n"; } const X opearator++(X& a,int) { //cout "X++ is called\n"; X b(a.i); a.i++; return b; } void f(X a) { } }; int main() { X a(1); f(a); a++; return 0; } Here when function 'f' is called copy constructor is getting called as expected. In case of a++, operator++ function is called but when it returns "copy constructor is not called". why "copy contructor is not called while returning from function

Initialization of a templated data member cryptic error

我是研究僧i 提交于 2019-12-25 09:17:37
问题 This is my tmp.hpp : #include <cstdlib> #include <utility> #include <unordered_map> using namespace std; struct int_int_hasher { size_t operator()(pair<int, int> const& p) const { return static_cast<size_t>(p.first) << 32 | p.second; } }; template<class T, class H> class BiBag { unordered_map<T, uint, H> t_to_id_; }; And simple tmp.cpp : #include "tmp.hpp" class tmp { BiBag<pair<int, int>, int_int_hasher> tt = BiBag<std::pair<int, int>, int_int_hasher>(); }; The error message is beyond my

C++ calling another class constructor

孤人 提交于 2019-12-25 09:17:07
问题 I am starting to learn about C++ classes and I have a problem. I read about constructors and initialization lists, but I still can't manage to solve my problem. Code in foo.h: class point{ public: double x,y; point(double x1, double y1); }; class line: public point{ public: double A,B,C; double distance(point K); line(point M, point N); }; And in foo.cpp: point::point(double x1, double y1){ x=x1; y=y1; } line::line(point M, point N){ if(M.x!=N.x){ A=-(M.y-N.y)/(M.x-N.x); B=1; C=-(M.y-A*M.x);

Curried constructor of generic class

时光怂恿深爱的人放手 提交于 2019-12-25 08:26:19
问题 Let me preface this by saying that I am not sure if this is possible. I am trying to obtain a constructor function that can be invoked with new that takes no parameters that calls a generic class's constructor that does take parameters. Like so: class SimpleFoo { public Key: String = null; } class GenericFoo<T> { public Key: T = null; constructor(private type: { new (): T }) { this.Key = new this.type(); } } let simpleCtor: { new (): SimpleFoo } = SimpleFoo; // works let simpleObj = new

C++ Instantiate, inside another class, a class-type variable from a group of classes with same constructor

只谈情不闲聊 提交于 2019-12-25 08:09:27
问题 I am not advanced in C++. Suppose I have a group of classes, from A to whatever (the number will grow in time), that use the same type of constructor. Suppose it looks like this: class A { private: double m_x, m_y; public: A(const double &x, double &y, const short &n) { ... }; }; Each of these classes have the same m_x, m_y variables, but they calculate it differently. Now there's another class, say Bla , who needs to use the constructors from the previous group of classes, something like