|
TasteMyHouse posted:The way I understand it, use a shared_ptr unless doing so would cause a cyclic reference, in which case use a weak_ptr -- but I'm actually not sure what you think isn't clear cut about passing a smart pointer into a function and I'm curious to hear you elaborate. I guess I'm just not really sure whether, in practice, accidental cyclic references happen often enough to justify not using shared_ptrs by default.
|
# ? Aug 10, 2011 21:29 |
|
|
# ? May 19, 2024 09:15 |
|
GrumpyDoctor posted:When using (TR1) smart pointers, which is a better rule of thumb: use shared_ptrs unless you have a reason not to, or use weak_ptrs unless you have a reason not to? My rule of thumb is to use a scoped_ptr whenever you want and avoid shared_ptrs like the plague, because the last thing we want is to have something destroyed on the wrong thread, so what's the point of shared_ptrs? Also shared_ptrs have a high overhead and if you want to share your pointers, use intrusive_ptr. But we aren't you and your needs are different.
|
# ? Aug 10, 2011 22:46 |
|
Say you have some function:code:
Here's a blog post about passing pointers by reference in case you are unfamiliar with the syntax. http://markgodwin.blogspot.com/2009/08/c-reference-to-pointer.html
|
# ? Aug 10, 2011 22:55 |
|
Dren posted:Say you have some function: No, void doStuff(void **memory) is the sane way to do this. Personally I have an idiomatic and inscrutable method for distinguishing between when it's appropriate to pass by reference and to pass a pointer.
|
# ? Aug 10, 2011 23:12 |
|
Dren posted:Say you have some function: I don't really see a problem with it, although I'd be more inclined to do this: code:
|
# ? Aug 11, 2011 00:06 |
|
wellwhoopdedooo posted:I don't really see a problem with it, although I'd be more inclined to do this: That's worse because that makes it more likely for the API to be misused. The former technique of void*& or void** is a way of implying to people using the function that the old pointer value is to be discarded and thou shalt never use it thygain.
|
# ? Aug 11, 2011 00:24 |
|
I need to append two int's together not in the sense of 1 + 2 = 3 but as 1 + 2 = 12. I know how to do this with strings but is there an easy way to do it with int's? I tried googling this but I can't find the proper way to word it.
|
# ? Aug 11, 2011 01:17 |
|
Alters posted:I need to append two int's together not in the sense of 1 + 2 = 3 but as 1 + 2 = 12. I know how to do this with strings but is there an easy way to do it with int's? Convert them to strings, concatenate them, convert back to integer. Edit: std::stringstream s; s << 1 << 2; std::string t = s.str(); shrughes fucked around with this message at 01:39 on Aug 11, 2011 |
# ? Aug 11, 2011 01:34 |
|
shrughes posted:Convert them to strings, concatenate them, convert back to integer. Or, you know, just use math: code:
Cubiks fucked around with this message at 04:45 on Aug 11, 2011 |
# ? Aug 11, 2011 01:48 |
|
Cubiks posted:Or, you know, just use math: But first you have to compute the log10 of the number.
|
# ? Aug 11, 2011 01:54 |
|
Cubiks posted:Or, you know, just use math: Don't think that would work if n2 was 10 or higher but in my case it's only 0-9 so that works perfectly. Thanks for the help guys. I should have stated that the number being added to the first one will never be greater than 9. Alters fucked around with this message at 02:26 on Aug 11, 2011 |
# ? Aug 11, 2011 02:00 |
|
I've been giving a polymorphic class:code:
code:
Right now I'm using the subclasses constructor like this: Course * ftc = new FullTimeCourse(fee,courseid); But the the fee can't be set from within the subclass because it's private to course. I'm not allowed to change the implementation of this either. Nor can the report function access that fee.
|
# ? Aug 11, 2011 04:26 |
|
Oxyclean posted:How am I supposed to access m_fee? it's private to Course, but every course needs to have a fee and id. You can either have a function on the base class that accesses it for your subclass, or you can make the field protected.
|
# ? Aug 11, 2011 05:08 |
|
The way you would set it is to make your constructor pass the value down to the base class constructor. You do that by naming the base class in the base/member initializers list:code:
|
# ? Aug 11, 2011 05:14 |
|
e: Nm, figured it out; had to put the ": Course(fee)" after my constructor in the subclass e: Of course someone responds just as I fix my mistake, haha. Thanks much though. Oxyclean fucked around with this message at 05:47 on Aug 11, 2011 |
# ? Aug 11, 2011 05:38 |
|
He's actually talking about the initializer list on the constructor itself. The relevant line in the example:code:
|
# ? Aug 11, 2011 05:43 |
|
Hughlander posted:Visual Studio question... In Visual Studio .NET, you go to the properties page of the project, then: C/C++ -> Preprocessor -> Preprocessor Definitions I think you're using VS2008? It's been years since I used that version, but I believe it's in the same place...
|
# ? Aug 11, 2011 09:05 |
|
rjmccall posted:But yeah, there's no legitimate way to actually read it given that class signature, so tough luck there.
|
# ? Aug 11, 2011 15:33 |
|
Plorkyeran posted:You can do a binary search for it assuming that's how operator< and operator> are defined . If you knew how the compiler laid out the object you could do some aliasing magic to read it.
|
# ? Aug 11, 2011 15:36 |
|
I'm told that the pure virtual function of the parent class (Course) can have a body and access m_fee. How would I then use that? From the subclass: code:
Oxyclean fucked around with this message at 16:15 on Aug 11, 2011 |
# ? Aug 11, 2011 16:10 |
|
shrughes posted:No, void doStuff(void **memory) is the sane way to do this. Personally I have an idiomatic and inscrutable method for distinguishing between when it's appropriate to pass by reference and to pass a pointer. i.e. code:
code:
Anyway, using a void *& argument seems inadvisable in C++ since it's probably eating memory and spitting memory back out and C++ has safer ways of dealing with memory.
|
# ? Aug 11, 2011 16:21 |
|
Oxyclean posted:I'm told that the pure virtual function of the parent class (Course) can have a body and access m_fee. You can't use an initializer list for anything but a constructor. You can, however, define a function body for Course's Report method. So you define: code:
|
# ? Aug 11, 2011 16:37 |
|
Oxyclean posted:I'm told that the pure virtual function of the parent class (Course) can have a body and access m_fee. But if it has a body, then it's not pure, virtual Are you sure your instructor isn't just loving with you? Anyways, you can do something like this: code:
Gerblyn fucked around with this message at 16:40 on Aug 11, 2011 |
# ? Aug 11, 2011 16:37 |
|
Well "virtual void Report()=0;" in the parent class means it's pure virtual right? But quote:void FullCourse::Report() Oxyclean fucked around with this message at 16:48 on Aug 11, 2011 |
# ? Aug 11, 2011 16:44 |
|
Gerblyn posted:But if it has a body, then it's not pure, virtual Are you sure your instructor isn't just loving with you? Oxyclean is for sure getting hosed with. For a minute I was going to suggest that he hide the double in the last 4 bytes of the char array. I would never think to implement a pure virtual function without changing the function prototype. Doing so is both dirty and wrong. Are you allowed to change the FullTimeCourse class definition? You could add the virtual keyword to the Report method and do: code:
Dren fucked around with this message at 16:54 on Aug 11, 2011 |
# ? Aug 11, 2011 16:45 |
|
In C++ you can define member functions that are declared as "pure virtual." You can even do it inline!code:
code:
|
# ? Aug 11, 2011 17:17 |
|
[quote="Mr.Radar"]code:
|
# ? Aug 11, 2011 17:46 |
|
Dren posted:Are you allowed to change the FullTimeCourse class definition? You could add the virtual keyword to the Report method and do: Methods with the same name and signature as a virtual method in a base class are implicitly virtual; they don't need to be redeclared as such, although it's good style to do so. ETA: Mr.Radar posted:In C++ you can define member functions that are declared as "pure virtual." You can even do it inline! That's an MSVC extension. rjmccall fucked around with this message at 17:57 on Aug 11, 2011 |
# ? Aug 11, 2011 17:46 |
|
rjmccall posted:Methods with the same name and signature as a virtual method in a base class are implicitly virtual; they don't need to be redeclared as such, although it's good style to do so. I take it destructors are a special case?
|
# ? Aug 11, 2011 17:57 |
|
I don't think so. As far as I know, if the base class has a virtual destructor, all the child classes do as well. Also, add me to the list of people who didn't know that MSVC let you define a body for a pure virtual method. Edit: Virtual destructors are sort of a special case I guess, since you don't need to explicitly call the base destructor from the derived destructor; The system does it automatically. Gerblyn fucked around with this message at 18:09 on Aug 11, 2011 |
# ? Aug 11, 2011 18:04 |
|
It's a special case if you think of the name of the destructor as including the class name, yes.
|
# ? Aug 11, 2011 18:08 |
|
Gerblyn posted:In Visual Studio .NET, you go to the properties page of the project, then: I know, but it's not listed there, and when I click on Command Line at the end of the C/C++ list, it's not listed there. However it is in the .vcproj file, and if I look at the obj file created, it shows up along with the rest of the command lines. I'm at an utter loss.
|
# ? Aug 11, 2011 18:40 |
|
Is it possible that the project file is corrupted somehow? I don't know the scope of what you're working with, but maybe you could just create a new project and move all your code into it?
|
# ? Aug 11, 2011 19:02 |
|
Anyone have a good recommendation for learning how to debug C applications and libraries on Windows? I can step through the execution of a program in VS no problem, but was never taught how to interpret a crash dump or how to properly build debug binaries to ensure crash dumps are useful. This knowledge would really come in useful right now. Thanks in advance.
|
# ? Aug 11, 2011 19:14 |
|
Hughlander posted:I know, but it's not listed there, and when I click on Command Line at the end of the C/C++ list, it's not listed there. However it is in the .vcproj file, and if I look at the obj file created, it shows up along with the rest of the command lines. I'm at an utter loss.
|
# ? Aug 11, 2011 19:27 |
|
What on earth is the purpose of that MSVC extension ? I cannot think of a single use case for that.
|
# ? Aug 11, 2011 19:30 |
|
GrumpyDoctor posted:What on earth is the purpose of that MSVC extension ? I cannot think of a single use case for that. But it's pretty contrived and unnecessary, since you could get that behavior by just having a non-virtual TreeNode::BeginClimb() function or something in addition to the pure virtual one.
|
# ? Aug 11, 2011 19:33 |
|
Yeah, you could just name it something else.
|
# ? Aug 11, 2011 19:35 |
|
I assume it's because you need to be able to implement pure virtual destructors, so they just extended it to any kind of member function.
|
# ? Aug 11, 2011 19:41 |
|
|
# ? May 19, 2024 09:15 |
|
Gerblyn posted:
All compliant compilers do. You can always define the body of a pure virtual function. The Microsoft extension he was talking about was that you can define them inside the class definition -- normally you have to define them outside of the definition.
|
# ? Aug 11, 2011 19:47 |