Click here to Skip to main content
15,889,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
{
	private:
		int day;
		string month;
	public:				
		DayOfYear(){}
		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";
			}
            else{
                 "Invalid Input";
            }
           return 0;
         }
};
Posted
Updated 22-Jul-19 8:39am

1 solution

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.
 
Share this answer
 
Comments
Afzaal Ahmad Zeeshan 22-Jul-19 17:21pm    
5ed.
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900