Question about constructor

Nov 6, 2012 at 2:43pm
Hi all,

I have a class like this:

1
2
3
4
5
6
7
8
class AClass
{
private:
    int k;
public:
    AClass(); //default constructor
    AClass dosth(); // random function
};

In the main function:

1
2
3
4
5
int main()
{
    AClass a=a.dosth();
    return 0;
}


Seems like the compiler will run the dosth function before the constructor, but I want the constructor to run first. I cannot edit the code in the main function because the main function is given by my teacher. Anything I can do?

Thanks in advance
Last edited on Nov 6, 2012 at 2:56pm
Nov 6, 2012 at 2:46pm
Seems like the compiler will run the dosth function before the constructor


This will never, ever happen. The constructor is always run first.

What makes you think dosth is running first?
Nov 6, 2012 at 2:53pm
I tried to run it step by step in debugging mode, it entered the dosth function first. The value of k is -856...
Nov 6, 2012 at 2:54pm
The code is invalid because function dosth have return type void. I did not test the code but I think the compiler shall issue an error.
Nov 6, 2012 at 2:56pm
 
 AClass a=a.dosth();


This code would not compile, as dosth returns void, not an AClass. Can you post the actual code you're using to call dosth? The only way it would call it first was if you are using an incomplete object (which really can only happen if you have a bad pointer)
Nov 6, 2012 at 2:57pm
Oh, sorry about that, that is my mistake. Because I wrote the code in the post section, I didn't notice it :D. That's just a random function and it returns a AClass data type.
Nov 6, 2012 at 3:06pm
Here is the class I'm working on:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Date
{
private:	
        int d, m, y;
public:
	Date();
	Date(int Y);
	Date(int Y, int M);
	Date(int Y, int M, int D);
	Date(const Date& src);
	Date& operator=(const Date& src);
	bool valid();
	Date Tomorrow();
	Date Yesterday();
	Date AddDay(int k);
	Date AddMonth(int k);
	Date AddYear(int k);
};


The code in the main function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void main()
{
	Date d1; 				// Current date: 2/11/2012
	Date d2(2012); 		// 1/1/2012;
	Date d3(2012, 8);		// 01/08/2012
	Date d4(2012, 10, 17); 	// 17/10/2012
	Date d5(d2);
	Date d6;
	d6=d3;

	d6=d3.Tomorrow();
	d5=d2.Yesterday();
	
	int k=7;
	Date d7=d7.AddDay(k);	//My problem is here, I cannot initialize d7
	d6=d2.AddMonth(k);		
	d2=d1.AddYear(k);   
        //More function.....

}

Last edited on Nov 6, 2012 at 3:09pm
Nov 6, 2012 at 3:12pm
Date d7=d7.AddDay(k);


Hrm... I'm surprised this compiles, but I guess I can see why it would.

Okay this line is your problem. You are constructing d7 with the output of d7.AddDay, so it cannot construct d7 without first calling AddDay, but it can't call AddDay with a constructed d7. See the problem?

I would've thought this would give you a compiler error, but maybe it doesn't. Huh.

The easiest solution is to just call the ctor first:

1
2
Date d7;  // constructs d7
d7 = d7.AddDay(k);  // now call AddDay 
Nov 6, 2012 at 3:18pm
I know it's the easiest way, but I cannot edit the main function like I mentioned.

I think the teacher gave a wrong code, my friend is stuck on this too.

Thank you guys anyway, keep it up :)
Nov 6, 2012 at 3:22pm
I cannot edit the main function like I mentioned.


Whoops. I missed that part. Sorry.

I think the teacher gave a wrong code, my friend is stuck on this too.


Yes, he did. There's nothing you can do about it if you can't change main. I'd bring it up with him.
Topic archived. No new replies allowed.