Click here to Skip to main content
14,387,787 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to use function of a class inside constructor. The function return type is object
which assign a value to one of private string through constructor. As function return an object, I got stucked there.

What I have tried:

class DayOfYear
		int day;
		string month;
		DayOfYear(int d){
			day = d;
			month = "";   // here, what I ve to put to get string i e 
 "January" or "Feb" from convert() function?
		DayOfYear convert()           
			if(day<=31 && day >=1)
				month = "January";
			else if(day<=59){
				day = day-31;
				month = "February";
                 "Invalid Input";
           return 0;
Updated 22-Jul-19 9:39am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You cannot call member instance functions from the object constructor - until the constructor has returned the object instance being constructed is "unfinished" and you can't use it or any instance function. It's like building a house: you can't go upstairs until the walls, floor, and roof are completed!

You can call static functions[^] however.
Afzaal Ahmad Zeeshan 22-Jul-19 17:21pm
Member 14520059 22-Jul-19 23:09pm
what do you mean?
OriginalGriff 23-Jul-19 2:23am
It's the reputation system here: see the "stars" above my reply?
He is saying "well done" and giving me "5 stars" which increases my reputation. Below three lowers it, above three increases it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100