11

Pass parameters to the constructor of a class that is a member of another class

 3 years ago
source link: https://www.codesd.com/item/pass-parameters-to-the-constructor-of-a-class-that-is-a-member-of-another-class.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Pass parameters to the constructor of a class that is a member of another class

advertisements

I have class called Bar, and in this class Bar there is an object of type Foo (a class). Class Bar takes 3 parameters, x, y and z. Foo takes 2 parameters, y and z.

currently I'm doing this:

class Bar {
 public:
  Bar(int x, int y, int z) {
      foo = new Foo(y, z);
      do something with x;
  }

 private:
  Foo * foo;
};

I remember seeing in a book another way to do this using a colon but I don't remember how exactly.

What is the standard or usual way of doing something like this?


Use an initializer list and avoid dynamic allocation:

class Bar {
public:
    Bar(int x, int y, int z) : foo(y, z) {}
private:
    Foo foo;
};

Related Articles

How do I pass parameters to the second-level top class

I am trying to understand inheritence and have some problems to understand. public class SiteTemplate extends SiteTemplateMethods public SiteTemplate(String country, String language, HttpServletRequest request){ super(); } public class SiteTemplateMe

use the constructor parameterized in the constructor of other classes

I fear that this is a very basic question, however, I was not able to solve it yet. I have a class A // classA.h ... class ClassA { public: ClassA(); ClassA(int foo); private: int _foo; ... } // classA.cpp ClassA::ClassA() { _foo = 0; } ClassA::Class

TestNG - pass parameters to the constructor where the parameters are decided according to the test name

I'm new to TestNG, here is my question.. I need to pass data to my @test methods via constructor, I've written the below code but getting a null pointer exception, public class ParameterTests { String x=null; int y; //no-arg constructor public Parame

How does the concept of a class change when passing data to the constructor instead of the method's parameters?

Let's say we're making a parser. One implementation could be: public sealed class Parser1 { public string Parse(string text) { ... } } Or we could pass the text to the constructor instead: public sealed class Parser2 { public Parser2(string text) { t

Providing passing arguments to the constructor in different public classes

I am having difficulty passing the hours and hourlyWage arguments to the constructor in the Paycheck class. The issue is as follows: symbol: variable hours location : class Paycheck It repeats for every instances of hours or hourly wage in public cla

Call the class method using Reflection with different parameters in the constructor?

Hello i am very new to c# reflection and i have the following problem. I have a CommandDispatcher class that invokes a certain Command class that does something. For now i use a switch to determinate what command to invoke. i wanted to simplify the c

Laravel how to pass parameters to the controller's middleware

I'm trying to do something but i cant even imagine how to do it. To pass parameters to a middlware I'm doing this: Route::put('post/{id}', ['middleware' => 'role:editor', function ($id) { // }]); From the laravel documentation. Then get the parameter

How to pass parameters to the REST resource using Jersey 2.5

I have a Java server which serves my clients (Not application server). Now I'm interested to add REST support. I've initialized a Jetty server and created few REST resources. My question is: How can I pass parameters at the creation of the REST resou

Best way to write the constructor of a class that holds an STL container in C ++ 11

class Foo { std::vector<SomeType> data_; }; Say Foo can only be constructed by make a copy (technically I mean a copy or move) of a std::vector<SomeType> object. What's the best way to write constructor(s) for Foo? My first feeling is Foo(std:

Need help understanding the parameters of the constructor and the static variables

This is a simple program #include <iostream> using namespace std; class Employee { public: Employee(string="default", int=10){}; void display(); private: static int x; static string s; }; int Employee::x=7; string Employee::s="Johnson

How to pass parameters from the nice URL to JSP pages?

I trying to do two things at once and I don't seem to find answers that can do both. I'm trying this: I want URLs ending in /user/{parameter} to be mapped to a JSP page user.jsp where I can access the parameter. I find ways to pass parameters from th

Why is the constructor of a class that implements the Runnable interface not called?

I tried using the constructor of a class which implements Runnable Interface. But I was surprised to see it was never called. The run() method was called, however, the constructor was never called. I have written a simple sample code to show the phen

Python bottle: how to pass parameters in the function handler

I am trying to call a function when an http GET request is sent to a specific route, but I would like to pass parameters to the given function. For example, I have the following: self.app.route('/here', ['GET'], self.here_method) where self.here_meth

Pass parameters in the URL (not with php GET)

Possible Duplicate: mod rewrite and query strings The only way I know to pass parameters in the URL is using the GET method in PHP. But I saw many websites using what seems to be parameters directly in the URL, like this: http://.../page/2/ In this c

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK