Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are
spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the
password reset link.
Entire forum
➜ Programming
➜ General
➜ Forcing the Explicit Constructor
Forcing the Explicit Constructor
|
It is now over 60 days since the last post. This thread is closed.
Refresh page
Pages: 1
2
Posted by
| Terry
USA (87 posts) Bio
|
Date
| Reply #15 on Tue 17 Nov 2009 10:20 PM (UTC) |
Message
|
Nick Gammon said:
You are storing shorts (16 bits) but getting an int (32 bit) as the argument. You are just asking for compiler warnings here.
I don't know why I'm doing it either. :D Fixed... I guess this is what happens when you do an assignment the day before its due, when you're half asleep. >_> You run into bugs everywhere, you make sloppy mistakes, and you stumble upon rather crazy solutions to problems that don't even exist. XD (Oh, and then you start pulling out your hair and scratching off your eyes when the compiler doesn't obey. :P)
In any case, I realized that, because I put default arguments in my constructor, it became the default constructor. The reason is, if you tried to overload the constructor with the default, you'd get yelled at for an ambiguous overload. So Dice d1; would indeed work. >:C I'm really sorry for wasting everyone's time. D:
Oh, and about the function creation thingy, it's because you CAN'T call an empty constructor. The way the C++ naming process works is that it can read A foo() as function foo which returns an object of class A , or it might be a constructor. It defaults to the first one (probably for the backward compatibility stuff, although it is probably also because functions are much more common than constructors).
About the *_dice , I guess I'll just have to make a vector. I was attempting to dynamically create an integer array via pointers, but the data gets cleaned, so the pointer becomes null. =\ Oh well. :)
Anyway, thanks for the help. :D And I'm sorry again for wasting time, but I appreciate all your work. :D | Top |
|
Posted by
| Nick Gammon
Australia (23,102 posts) Bio
Forum Administrator |
Date
| Reply #16 on Wed 18 Nov 2009 12:59 AM (UTC) |
Message
| No worries.
By the way, you *can* dynamically create arrays using the Standard Template Library (STL). I have a heap of posts about it on this forum.
Something like:
Then you can put as many items as you like into it with push_back. You don't need to impose any arbitrary limits.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|
Posted by
| Twisol
USA (2,257 posts) Bio
|
Date
| Reply #17 on Wed 18 Nov 2009 01:12 AM (UTC) |
Message
| More simply, you can create arbitrary-size arrays at runtime just with dynamic allocation, i.e.
int[] int_arr = new int[ARR_SIZE];
The memory referred to by int_arr is stored on the heap, not the stack, so you can store the pointer and use it elsewhere. However you do need to be sure to call delete[] on it when you're done with it, i.e.
I prefer vectors, as Nick showed, when I don't have a hard limit to the amount of data I'm storing, or when there's no reason to limit it in the first place. But if I was just going to store the results of exactly 64 dice rolls, I'd probably use an array. |
'Soludra' on Achaea
Blog: http://jonathan.com/
GitHub: http://github.com/Twisol | Top |
|
Posted by
| David Haley
USA (3,881 posts) Bio
|
Date
| Reply #18 on Wed 18 Nov 2009 07:41 PM (UTC) |
Message
|
Terry said: Oh, and about the function creation thingy, it's because you CAN'T call an empty constructor.
What do you mean? If you have a class A with a constructor that takes no parameters (or all parameters have default values) then you can do things like:
A a = A();
which is calling the "empty" constructor. (Assuming that's what you mean by "empty" here.)
Terry said: It defaults to the first one (probably for the backward compatibility stuff, although it is probably also because functions are much more common than constructors).
It's the only way to write a forward declaration of a function that takes no parameters, so there isn't really any other option. |
David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone
http://david.the-haleys.org | Top |
|
Posted by
| Terry
USA (87 posts) Bio
|
Date
| Reply #19 on Fri 20 Nov 2009 06:15 PM (UTC) |
Message
| Vectors are what I ended up using in the end, and I also ended up taking out and moving around and doing a whole bunch of stuff with a whole bunch of stuff to basically make it so my program completely changed. :P If anyone's interested, I can post my work so you guys can see my end result, but it's not too terribly exciting imo. :D | Top |
|
The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).
To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.
53,665 views.
This is page 2, subject is 2 pages long:
1
2
It is now over 60 days since the last post. This thread is closed.
Refresh page
top