Search Results

Search found 19928 results on 798 pages for 'multiple constructors'.

Page 24/798 | < Previous Page | 20 21 22 23 24 25 26 27 28 29 30 31  | Next Page >

  • Error handling in C++, constructors vs. regular methods

    - by Dennis Ritchie
    I have a cheesesales.txt CSV file with all of my recent cheese sales. I want to create a class CheeseSales that can do things like these: CheeseSales sales("cheesesales.txt"); //has no default constructor cout << sales.totalSales() << endl; sales.outputPieChart("piechart.pdf"); The above code assumes that no failures will happen. In reality, failures will take place. In this case, two kinds of failures could occur: Failure in the constructor: The file may not exist, may not have read-permissions, contain invalid/unparsable data, etc. Failure in the regular method: The file may already exist, there may not be write access, too little sales data available to create a pie chart, etc. My question is simply: How would you design this code to handle failures? One idea: Return a bool from the regular method indicating failure. Not sure how to deal with the constructor. How would seasoned C++ coders do these kinds of things?

    Read the article

  • C++ using this pointer in constructors

    - by gilbertc
    In c++, during a class constructor, I started a new thread with 'this' pointer as a parameter which will be used in the thread extensively (say, calling member functions). Is that a bad thing to do? Why and what are the consequences? Thanks, Gil.

    Read the article

  • Calling private constructors with Reflection.Emit?

    - by Jakob Botsch Nielsen
    I'm trying to emit the following IL: LocalBuilder pointer = il.DeclareLocal(typeof(IntPtr)); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Stloc, pointer); il.Emit(OpCodes.Ldloca, pointer); il.Emit(OpCodes.Call, typeof(IntPtr).GetMethod("ToPointer")); il.Emit(OpCodes.Ret); The delegate I bind with has the signature void* TestDelegate(IntPtr ptr) It throws the exception Operation could destabilize the runtime. Anyone knows what's wrong? EDIT: Alright, so I got the IL working now. The entire goal of this was to be able to call a private constructor. The private constructor takes a pointer so I can't use normal reflection. Now.. When I call it, I get an exception saying Attempt by method <built method> to access method <private constructor> failed. Apparently it's performing security checks - but from experience I know that Reflection is able to do private stuff like this normally, so hopefully there is a way to disable that check?

    Read the article

  • Are protected constructors considered good practice?

    - by Álvaro G. Vicario
    I'm writing some little helper classes to handle trees. Basically, I have a node and a special root node that represents the tree. I want to keep it generic and simple. This is part of the code: <?php class Tree extends TreeNode{ public function addById($node_id, $parent_id, $generic_content){ if( $parent = $this->findNodeById($parent_id) ){ $parent->addChildById($node_id, $generic_content); } } } class TreeNode{ public function __construct($node_id, $parent_id, $generic_content){ // ... } protected function addChildById($node_id, $generic_content){ $this->children[] = new TreeNode($this->node_id, $node_id, $generic_content); } } $Categories = new Tree; $Categories->addById(1, NULL, $foo); $Categories->addById(2, NULL, $bar); $Categories->addById(3, 1, $gee); ?> My questions: Is it sensible to force TreeNode instances to be created through TreeNode::addById()? If it's so, would it be good practise to declare TreeNode::__construct() as private/protected?

    Read the article

  • Java - Thread safety of ArrayList constructors

    - by andy boot
    I am looking at this piece of code. This constructor delegates to the native method "System.arraycopy" Is it Thread safe? And by that I mean can it ever throw a ConcurrentModificationException? public Collection<Object> getConnections(Collection<Object> someCollection) { return new ArrayList<Object>(someCollection); } Does it make any difference if the collection being copied is ThreadSafe eg a CopyOnWriteArrayList? public Collection<Object> getConnections(CopyOnWriteArrayList<Object> someCollection) { return new ArrayList<Object>(someCollection); }

    Read the article

  • How best to implement support for multiple devices in a web application.

    - by Kabeer
    Hello. My client would like a business application to support 'every possible device'. The application in question is essentially a web application and 'every possible device', I believe encompasses mobile phones, netbooks, ipad, other browser supporting devices, etc. The application is somewhat complex w.r.t. the data it captures and other functions it performs (reporting). If I continue to honor increasing complexity in the application, I guess there are more chances of it not working on other devices. I'd like to know how web applications support multiple devices conventionally? Are there multiple versions of presentation layer (like many times I find m.website.com dedicated for mobile devices)? Further, if my application is to take advantage of Java Script, RIA (Flash, SilverLight) then what are the consequences and workarounds? Mine is a .Net based application and the stack also contains Ext JS Java Script library. While I would like to use it for sure, considering that I would be doing a lot of work in Java Script rather than HTML, this could be a problem. The answer to the above could be descriptive. If there is something already prescribed out there, please share the link(s). Thanks.

    Read the article

  • How to configure a NSPopupButton for displaying multiple values in a TableView?

    - by jekmac
    Hi there! I'm using two entities A and B with to-many-to-many relationship. Lets say I got an entity A with attribute aAttrib and a to-many relationship aRelat to another entity B with attribute bAttrib and a to-many relationship bRelat with entity A. Now I am building an interface with two tables one for entity A and another for entity B. The table for entity B has two columns one for bAttrib and one for the relationship aRelat. The aRelat-column should be a NSPopupButtonCell to display multiple aAttrib values. I'd like to set all the bindings in InterfaceBuilder in Table Column Bindings: -- I have two NSArrayController each for one entity: Object Controller Mode:Entity Array Controller Bindings: Parameters Managed Object Context bind to File's Owner -- One Table Cloumn with a PopUpButtonCell: TableCloumnBindings Content bind to Entity A with ControllerKey arrangedObjects; Content Values bind to Entity A with ModelKeyPath aAttrib Selected Object bind to Entity B with ModelKeyPath bRelat I know that this configuration doesn't allow multiple value setting. But I don't know how to do the right one. Getting the following message: HIToolbox: ignoring exception 'Unacceptable type of value for to-many relationship: property = "bRelat"; desired type = NSSet; given type = NSCFString; value = testValue.' that raised inside Carbon event dispatch... Does anyone have any idea?

    Read the article

  • Constructors for C++ objects

    - by sasquatch
    I have class Person as following : class Person { char* name; int age; }; Now I need to add two contructors. One taking no arguments, that inserts field values to dynamically allocated resources. Second taking (char*, int) arguments initialized by initialization list. Last part is to define a destructor showing information about destroying objects and deallocating dynamically allocated resources. How to perform this task ? That's what I already have : class Person { char* name; int age; public: Person(){ this->name = new *char; this->age = new int; } Person(char* c, int i){ } };

    Read the article

  • JavaScript - Inheritance in Constructors

    - by j0ker
    For a JavaScript project we want to introduce object inheritance to decrease code duplication. However, I cannot quite get it working the way I want and need some help. We use the module pattern. Suppose there is a super element: a.namespace('a.elements.Element'); a.elements.Element = (function() { // public API -- constructor Element = function(properties) { this.id = properties.id; }; // public API -- prototype Element.prototype = { getID: function() { return this.id; } }; return Element; }()); And an element inheriting from this super element: a.namespace('a.elements.SubElement'); a.elements.SubElement = (function() { // public API -- constructor SubElement = function(properties) { // inheritance happens here // ??? this.color = properties.color; this.bogus = this.id + 1; }; // public API -- prototype SubElement.prototype = { getColor: function() { return this.color; } }; return SubElement; }()); You will notice that I'm not quite sure how to implement the inheritance itself. In the constructor I have to be able to pass the parameter to the super object constructor and create a super element that is then used to create the inherited one. I need a (comfortable) possibility to access the properties of the super object within the constructor of the new object. Ideally I could operate on the super object as if it was part of the new object. I also want to be able to create a new SubElement and call getID() on it. What I want to accomplish seems like the traditional class based inheritance. However, I'd like to do it using prototypal inheritance since that's the JavaScript way. Is that even doable? Thanks in advance! EDIT: Fixed usage of private variables as suggested in the comments. EDIT2: Another change of the code: It's important that id is accessible from the constructor of SubElement.

    Read the article

  • Constructors with inheritance in c++

    - by Crystal
    If you have 3 classes, with the parent class listed first shape- 2d shapes, 3d shapes - circle, sphere When you write your constructor for the circle class, would you ever just initialize the parent Shape object and then your current object, skipping the middle class. It seems to me you could have x,y coordinates for Shape and initialize those in the constructor, and initialize a radius in the circle or sphere class, but in 2d or 3d shape classes, I wouldn't know what to put in the constructor since it seems like it would be identical to shape. So is something like this valid Circle::Circle(int x, int y, int r) : Shape(x, y), r(r) {} I get a compile error of: illegal member initialization: 'Shape' is not a base or member So I wasn't sure if my code was legal or best practice even. Or if instead you'd have the middle class just do what the top level Shape class does TwoDimensionalShape::TwoDimensionalShape(int x, int y) : Shape (x, y) {} and then in the Circle class Circle::Circle(int x, int y, int r) : TwoDimensionalShape(x, y), r(r) {}

    Read the article

  • Calling constructors in c++ without new

    - by Nils
    I've often seen that people create objects in C++ using Thing myThing("asdf"); Instead of Thing myThing = myThing("asdf"); This seems to work (using gcc), at least as long as there are no templates involved. My question now, is the first line correct and if so should I use it?

    Read the article

  • C++ allocate objects on heap of base class with protected constructors via inheritance

    - by KRao
    I have a class with protected constructor: class B { protected: B(){}; }; Now I derive from it and define two static functions and I manage to actually create objects of the class B, but not on the heap: class A : public B { public: static B createOnStack() {return B();} //static B* createOnHeap() {return new B;} //Compile time Error on VS2010 }; B b = A::createOnStack(); //This works on VS2010! The question is: 1) Is VS2010 wrong in allowing the first case? 2) Is it possible to create objects of B without modifying B in any way (no friendship and no extra functions). I am asking, because it is possible to make something similar when dealing with instances of B and its member functions, see: http://accu.org/index.php/journals/296 Thank you in advance for any suggestion! Kind regards

    Read the article

  • JavaScript constructors inside a namespace

    - by Joe
    I have read that creating a namespace for JavaScript projects helps to reduce conflicts with other libraries. I have some code with a lot of different types of objects for which I have defined constructor functions. Is it good practice to put these inside the namespace as well? For example: var shapes = { Rectangle: function(w, h) { this.width = w; this.height = h; } }; which can be called via: var square = new shapes.Rectangle(10,10);

    Read the article

  • Constructors in Inner classes (implementing Interfaces)

    - by thepandaatemyface
    Hi, How would I go about writing a constructor for an inner class which is implementing an interface? I know I could make a whole new class, but I figure there's got to be a way to do something along the line of this: JButton b = new JButton(new AbstractAction() { public AbstractAction() { super("This is a button"); } public void actionPerformed(ActionEvent e) { System.out.println("button clicked"); } }); When I enter this it doesn't recognize the AbstractAction method as a constructor (compiler asks for return type). Anyone have an idea? Thanks

    Read the article

  • C++ syntax of constructors " 'Object1 a (1, Object1(2))''

    - by osgx
    Hello I have a such syntax in program class Object1 : BaseClass { BaseClass *link; int i; public: Object1(int a){i=a;} Object1(int a, Object1 /*place1*/ o) {i=a; link= &o;} }; int main(){ Object1 a(1, /*place2*/ Object1(2)); ... } What do I need in place1? I want to save a link (pointer) to the second object in the first object. Should I use in place1 reference "&"? What type will have "Object1(2)" in place2? Is it a constructor of the anonymous object? Will it have a "auto" storage type?

    Read the article

  • c++ constructors

    - by aharont
    i wrote this code: class A { public: A(){d=2.2;cout<<d;} A(double d):d(d){cout<<d;} double getD(){return d;} private: double d; }; class Bing { public: Bing(){a=A(5.3);} void f(){cout<<a.getD();} private: A a; }; int main() { Bing b; b.f(); } i get the output: 2.2 5.3 5.3 instead of 5.3 5.3. it's something in the constructor.... wahy am i getting this? how can i fix it?

    Read the article

< Previous Page | 20 21 22 23 24 25 26 27 28 29 30 31  | Next Page >