Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Anunnaki posted:

Err, I'm sorry, I'm just going by what my IDE calls it; what I mean is whether or not you begin a new line for the opening bracket for any multi-line function, like an if statement, or whatever. Whoknew's code was written with the style where the opening bracket starts on the same line.

As long as you consistently use a single sane style, if they seriously care which style it is you probably don't want to work for them.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

ultra-inquisitor posted:

Obviously I shouldn't be passing the address of donk to blah(), but I don't understand the behaviour when I did. What seems to happen is that, in blah(), foo is cast to some kind of phantom Obj* which has not been constructed, but doesn't segfault when used.

Why would it segfault? On most platforms an Obj and a pointer to an Obj are the same size, so you're passing blah an allocated chunk of memory of the correct size. If Obj had more than one field or if on your platform ints and pointers were different sizes this wouldn't "work".

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
What does an abstract class with no public interface give you over just not inheriting at all? void doStuff(VehicleDealership vd) { ... } can't actually do anything with vd.

floWenoL posted:

What if someone has a PorscheDealership but add()s a non-Porsche car?

It'd fail to compile because there is no add(Car *)?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Getting torn to pieces is one of the most effective ways of learning from people on the internet due to how many people are far more willing to put effort into explaining why you're an idiot than answering a question. This has some unfortunate implications for people who don't like endless trolling, though.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Avenging Dentist posted:

for instance, cplusplus.com actually asserts that arrays are pointers.
I had so many people tell me that when I was learning C++ :(

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
If you're fine with being windows-only, just use DirectShow. For other platforms, ffmpeg is the only particularly functional option, which you can use either by using libavcodec directly or by using a wrapper such as FFmpegSource.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Why would you use longjmp/inline asm instead of just generating machine code which follows the appropriate calling convention then setting a function pointer to the appropriate address and calling it?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
http://en.wikipedia.org/wiki/Object_slicing

Make ObjectManager::AddObj take a pointer to an Object instead and it'll work

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
That's perfectly legal and there's even semi-valid reasons to want to do it, such as Duff's device. Switch requires that the first case statement (or default) comes before any expressions in switch's body, but nothing says that subsequent case statements cannot be inside other statements.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

MagneticWombats posted:

What do people think about http://www.stepanovpapers.com/notes.pdf ? It's less mathy than most of his other stuff and it's kind of fun (and on a level, sad) to see the guy who did STL rail against the standards committee. On the other hand, for all his regrets, I wonder why he never made his own language or something.
Are there answers for the quiz questions anywhere? Most of them are pretty obvious but there's a few I'm not confident about.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Copying and pasting comments is much worse than "silly". I think I'd describe it as more of "actively malicious", as it's pretty much guaranteed to result in incorrect documentation in one of the files at some point in the future.

Personally I'd consider documenting stuff related to the public interface in the header file and everything else in the source file to be the only particularly sane option.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
The STL does have bind1st and bind2nd.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
They're awesome and fix most of my complaints with the language. I can't wait for a compiler to implement them.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

jonjonaug posted:

Big hint: Everything up to and including "delete[] x;" is fine.
Almost everything. The second for loop past the end of x.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

cliffy posted:

Really? There is never a good reason to hide the implementation of a template?
There are certainly good reasons to want to hide the implementation of a template. That does not mean it is a good idea to actually do so, though.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

washow posted:

Hey goons! C++ student here!

This thing here http://pastebin.com/BGPSEEqJ compiles but I'm getting runtime error and can't quite see what's wrong with it

help?
What compiler are you using that accepts that program? That really shouldn't compile and it doesn't in gcc.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Ledneh posted:

On another topic, because it came up in a discussion with coworkers earlier: is there any real argument to deciding when to pass around const pointers vs references besides "only use pointers when polymorphism is involved or said item can be null"?
There are very few if any places where I consider non-const references appropriate, but this is mostly a stylistic thing.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Vanadium posted:

Worth pointing out that this only works within a class hierarchy with virtual destructors. :toot:
No, it doesn't. As has already been pointed out several times, virtual/non-virtual destructors have nothing to do with how much memory gets freed (other than that the derived destructor that doesn't get run might have needed to do something).

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Ledneh posted:

is it C++ standard behavior to do nothing on a delete of a NULL/0 pointer
Yes.

Ledneh posted:

why the hell have I been doing things like if (ptr) delete ptr; all these years? :saddowns:
Because of lovely books and tutorials.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Johann Gambolputty posted:

My question is if the debugger doesn't know the size, then how does the program know not to use the other addressed memory for the array.
It doesn't.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Define a strict total order for your class so that two different objects aren't equal.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Debug runtimes don't come preinstalled and aren't licensed for redistribution, so if you haven't explicitly installed them or Visual Studio on a computer you can't run debug builds which dynamically link the runtime.

Everything being linked together to form an executable needs to link against the same version of the runtime library. Switching only one thing between debug and release or static and dynamic runtime linking produces the linker errors you're seeing.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
In general, if you are linking against a library that you are compiling yourself you'll need to build both debug and release versions of it, and if you're linking against supplied binaries you have to hope that they're supplying binaries built in the configurations you need.

Note that if you toss all of your dependencies into the solution with your project and set your project to depend on them (rather than building them separately and explicitly passing the .lib to the linker), visual studio does 90% of the work of ensuring that you're linking against the right things for you.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
There's really not much of a difference.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Ledneh posted:

I haven't tired it, but wouldn't lowercasing the whole key then checking for string equality against the (already lowercase) map key amount to the same CPU load as lowercasing character by character (which we're doing now)?
Lowercasing the string first means you lowercase it once; lowercasing it in the comparison function means you lowercase it log N times. Unless you only have two items in your map, this is a pretty massive difference.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Don't inherit from std::string. It doesn't have a virtual destructor and you can't safely use a lowercase_string as a string (e.g. static_cast<std::string&>(str) += "A" would break the invariant that all letters in the lowercase_string are lowercase).

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
err.where<char>()

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

epswing posted:

Easy enough, but now this means to concat two strings, I can't just do "one" + "two", it needs to be
You can't do that even with non-wide strings, and return L"\\\\.\\pipe\\" + this->pipeName; works exactly the same as it would with no L and pipeName a std::string.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Ledneh posted:

One last (and more general) question, then. Does having a working garbage collector give the users carte blanche to comment out their deletes (and most destructors), since the GC will take care of it later? Or would counting on the GC as a rule cause a gross performance impact? (Or maybe even a performance boost, if destructors don't need to be called anymore? I don't know, I'm talking out of my rear end here.)
With a good GC it'll improve performance at the cost of memory usage as the GC can do the deleting when the program is idle. With a bad GC (or a program that is never idle), it'll potentially kill performance by doing the deleting at all the wrong times.

In most cases it'll have no noticeable impact.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Hammerite posted:

If you refer to it in an integer context, you get the number of elements. You can assign an integer to set the number of elements.
I realize this is probably what the book said to do for educational purposes, but keep in mind that wacky conversion operators like this are nearly always a terrible idea.

Hammerite posted:

The book states that if you provide two overloaded versions of the subscript operator, one marked const and the other not, then the compiler is clever enough to use the appropriate one depending on whether the context is a reading or writing one; but from some simple experiments this didn't seem to be the case. (Using the GCC compiler included with Code::Blocks.)
I've wanted this behavior before, but unfortunatly the book is wrong about this. You still need to provide both const and non-const versions, though.

Some things I noticed while reading the code:

C++ doesn't have a single nearly universal naming scheme like some languages do, but I've never seen camelCaps used for class names in C++ except for when it's a project-specific prefix.

display, summary, total, mean, variance, and standardDeviation should be free functions which consume a myArray rather than members of the class, as they don't need access to myArray's privates. Similarly, the comparison operators don't need to be friended.

You don't need the check for numberOfElements in the destructor, as deleting a NULL pointer is valid.

The comparison operators would be simplified by just calling memcmp rather than looping over the arrays.

Don't use const references to ints. It's slower and more typing than just passing an int by value and there's no advantage.

There should be an assignment operator that takes a myArray.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Yeah, you don't want to use memcmp; that was a dumb suggestion.

Hammerite posted:

How come? What are the two different versions used for?
Without a const version of operator[], you can't read from a const myArray, which means you can't usefully pass const references array or read from a myArray member in a const method of a different class. Without a non-const version, you can't actually assign to array elements.

Plorkyeran fucked around with this message at 04:00 on Nov 2, 2010

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Please don't use GTK+ if you're ever planning to use the program on anything but linux.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

ToxicFrog posted:

Why not? I've never had any problems releasing windows programs using GTK+.
Other than that they're hideous and probably ignore a lot of platform conventions?

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Fragaroc posted:

A pretty simple question I think but I can't figure out if I'm doing anything wrong.

I'm trying to create a method for the ExpTree class that returns a pointer to an object of type node and its input is a reference to an iterator.
code:
Node* subBuild(std::list<std::string>::iterator* &);
That function takes a reference to a pointer to an iterator.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

pr0metheus posted:

code:
void operator = (const SharedPtr & other )
{
    this->ptr = other.ptr;
    this->counter = other.counter;
    (*counter)++;
}
Think about what happens here when &other == this. This is one of the reasons why I generally prefer the copy-swap method for handling operator=.

Also, operator= should always return a reference to this so that chaining works.

pr0metheus posted:

I don't quite understand why -> operator works by just returning *. Can somebody explain semantics of it and why returning the pointer works?
operator-> simply returns a thing to use -> on instead (either a pointer or another object with overloaded ->); it doesn't completely replace the default functionality like most of the operators do. In practice this is enough for any sane usage of overloading ->, and it's the only remotely practical way to actually implement it.

Plorkyeran fucked around with this message at 23:25 on Dec 9, 2010

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
http://msdn.microsoft.com/en-us/library/bb432569.aspx is the documentation for the cabinet API. Getting a list of files in the cabinet appears to be fairly awkward: the extract all function (FDICopy) takes a callback which receives information about each file before it's extracted and can tell the extractor to skip extracting the file.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Harokey posted:

Is there a way to create an exception class, that when thrown (and not caught) prints a meaningful error message?
std::set_terminate is probably supported on all of the platforms that you have to care about. In the handler you assign you can rethrow the exception, then catch it and print a more useful message.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

GrumpyDoctor posted:

Is there an idiomatic workaround for the fact that you can't use remove_if on STL maps because you can't reorder them? (Or, if you can, how do you do it?)
There's no standard function, but it's not very hard to write one:
code:
template<class Container, class UnaryPredicate>
void erase_if(Container& cont, typename Container::iterator begin, typename Container::iterator end, UnaryPredicate pred) {
    while (begin != end) {
        if (pred(*begin)) {
            cont.erase(begin++);
        }
        else {
            ++begin;
        }
    }
}
Note that this works only for containers where erase does not invalidate other iterators.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Buffer overflows would be way less of an issue if they always caused crashes.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
Templates (and things made possible by templates) are the only thing that makes coding in C++ even vaguely tolerable.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply