constructor value at time of call

constructor list supplied at runtime vs before compiler
Example below - can I supply building_num{1200}, garages_num{23} at time of the call rather than before within the class definition?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class City
{
	private:
		int road_num;
		int house_num;
		int building_num;
		int garages_num;
		int streetlamps_num;
		int array[5];

	public:
		//City();  {}!!!!!
		City() : building_num{1200}, garages_num{23}, streetlamps_num{999}, array{4,3,7,3,100}
		{
			//nothing to do here
		}
Last edited on
Do you mean a constructor with parameters?
1
2
3
4
5
City(int num_buildings, int num_garages): building_num{num_buildings }, 
       garages_num{ num_garages }, streetlamps_num{ 999 }, array{4, 3, 7, 3, 100}
  {
    //nothing to do here
  }
Not really. I want to pass some system values on to the constructor of a small routine that will analyze them. Hypothetically the values will be available after run time. I want to run the program, call the function to get the values, then pass those to the analysis subroutine with the constructor.
Last edited on
hypothetically the values will be available after run time.
After run-time? Please explain.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class City
{
	private:
		int road_num;
		int house_num;
		int building_num;
		int garages_num;
		int streetlamps_num;
		int array[5];

	public:
		//City();  {}!!!!!
		City() : building_num{1200}, garages_num{23}, streetlamps_num{999}, array{4,3,7,3,100}
		{
			//nothing to do here
		}


In my example above building_num, garages_num, etc. are already known, no prob.
My program of concern checks for resolution of screen post-compile.
When it does that I want to pass it to an object for analysis. But how do I do this? I can't set up a constructor at this point can I?
Last edited on
I want to run the program, call the function to get the values, then pass those to the analysis subroutine with the constructor.


Well you can do that by passing the arguments to the constructor when you instantiate the object:

1
2
3
4
5
6
7
8
9
10
// next line is a function declaration
get_screen_data(unsigned int& height, unsigned int& width);

//function call
get_screen_data(height,  width);

// ScreenAnalyizer class has a constructor which takes 2 arguments
//instantiate  MyScreen object, calls  ScreenAnalyizer constructor with arguments
ScreenAnalyizer MyScreen(height, width);


Timing wise I think you mean at runtime
Last edited on
Your information allowed me to finally get a constructor working for my object, and it worked fine. Thank you. If you know anything about drawing in windows point me to the right direction, otherwise thanks !
I found what I needed (very dated though).
I had to add an old .h to my system though. Problem is I do know where in my include system to put it...

#include<graphics.h>
I found what I needed (very dated though).


IMO you would be much better off to use a modern GUI framework. There are a bunch of options - Qt , sfml probably others too.

Edit: Using something so old, is a bit like if you have a brand new car with Electronic Fuel Injection, but you want to retrofit a carburettor from a 1987 car :+(
Last edited on
ha ha. I found graphics.h, ended up going with SDL library though. Just wanted to draw a few lines -- it looks like some fun in doing it though. SDL way good 2D graphics though.thx
Topic archived. No new replies allowed.