Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Member Since 10 Apr 2010
Offline Last Active Dec 17 2012 02:42 PM

#4986933 Is that way of using constructor really better ?

Posted by Slavik81 on 04 October 2012 - 04:33 PM

If there is a compiler that does not issue a warning when you put initializers out of order, you should switch to one that does.

FYI Visual c++ 2012 does not issue a warning about this with /wall (and 'language extensions' disabled fwiw). I know gcc does, and I agree that it's a useful warning.

Yeah, msvc is the only compiler I know of that doesn't support that warning. For gcc, clang and intel, the flag is -Wreorder.

You could vote for it if you want Microsoft to add it.

#4984931 SFINAE equivalent in C#?

Posted by Slavik81 on 28 September 2012 - 08:12 PM

There is no such thing as "const" in C#, so that's a non issue.
However, it's normal that it doesn't work in c++. An array of const objects is not all all the same type as an array of non const objects. You can't expect implicit conversion of those two types.

I could expect it, given that the two objects should have both identical code and data, and one has an interface that is a subset of the other. The fact that it's so difficult to use one in place of the other is just a limitation imposed by poor design choices in the C++ template system.

If it C++ disallowed atrocities like this, the types would be compatible:
struct MyObject{};

template<class T> struct MyTemplateClass {
   T x;

template<> struct MyTemplateClass<const MyObject*>{};

int main()
   MyObject obj;

   MyTemplateClass<MyObject*> i1;
   i1.x= &obj;

   MyTemplateClass<const MyObject*> i2;
   i2.x= &obj; // compile error

Reading through stuff about C# generics, it seems that they maintain restrictions to prevent that sort of thing.

#4984901 #pragma once

Posted by Slavik81 on 28 September 2012 - 06:08 PM

I would use #pragma once because it's simpler and leaves less room for programmer error. It is supported across all major compilers (gcc, msvc, clang, intel...). In the event that you discover that you need to support an old compiler, you can run a script such as this one to replace #pragma once with include guards.

I've only once encountered a header file in which I wouldn't want to include #pragma once. By default, you should be doing it for every header file.

#4950315 Overusing smart pointers?

Posted by Slavik81 on 18 June 2012 - 11:42 AM

Assuming the pointer must not be null, is there any reason to use pointer.get() over &*pointer? I've been using the latter as it works for pretty much any pointer-like type (raw pointers, boost smart pointers, qt smart pointers).

#4947468 C++ what other IDE other than VC++

Posted by Slavik81 on 08 June 2012 - 02:27 PM

I don't think there's much risk of accidentally using signals and slots... But even if so, the code is still standard C++ (afaik, it's not an extension to the C++ language itself, they just have another pre-compile step that converts the signals and slots to valid C++). I think you can even compile code using signals and slots using another IDE (or another compiler), as long as you run it through the QMake build step first, but I'm not certain.

To use other build systems, all you need to do is run the moc over each class that declares signals or slots. Compile and link each of the moc's output files into the same library as the input file. There is no requirement even to use qmake.

#4926733 Avoiding Global Variables

Posted by Slavik81 on 30 March 2012 - 11:29 AM

In C#? I can believe that in C++. The product I'm working on (239k lines C#) currently builds in 4m52s, unit tests (1699) take 4m29s (based on my last commit)

Ah. In C++, I'd expect a build of that sort to take maybe 5 to 10 times that long. Your perspective suddenly makes a lot more sense to me.

#4926160 Efficient data structure for a maze

Posted by Slavik81 on 28 March 2012 - 05:35 PM

Rather than a 2D array of cells, I'd make a 2D array of walls. Then, when you query about a cell, you construct the cell using data from the array of walls. You can further optimize if you want, breaking your array into an array of smaller arrays to ensure spacial locality of memory.

That said, what you have now sounds fine to me. Unless performance is a problem, why optimize? Anything you do will have trade-offs, and unless you understand what resources are abundant and what resources are scarce in your program, you're going to be unable to make an informed decision.

#4925494 Are dirty coding techniques okay to use?

Posted by Slavik81 on 26 March 2012 - 06:35 PM

Occasionally, yes. But if you're going to take a short-cut, you should always carefully document what you've done and what needs to be done in order to fix things properly.

You're taking out a loan when you do stuff like that. Keep in mind that you'll eventually have to pay it back. With interest.

#4911002 C First?

Posted by Slavik81 on 08 February 2012 - 01:22 PM

First off, learn "C" in the context of "C++".

But... why???

The question is "learn C before C++?", take the baby steps into C++ but learn the core language "FIRST". All the rest of your arguments get the same answer: "because you need to learn the basics first".

I know what the question is, but I don't get what you mean by "learn the core language." What "core language?" C++ is the core language of C++. Not C.

int arr[] = { 1, 2, 3 };
int product = 1;
for(int i=0; i<3; i++)
   product = product * arr[i];
std::cout << product;

std::vector<int> vec;
int product = 1;
for(std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it)
   product = product * (*it);
std::cout << sum;

The C-style syntax is easier to pick up than STL C++ with its templates, namespaces and iterators. I'd make an exception for iostreams, since they're much simpler than format specifiers.

There's no reason to immediately push them into using <algorithms> or anything like that, even if it is more 'idiomatic' C++. They're better off ignoring most of the STL until they have a grasp of the basic concepts of the language. Take things one step at a time.

#4889153 Protecting Internal Classes

Posted by Slavik81 on 30 November 2011 - 12:37 PM

A comment is often sufficient. The first time they go to try to construct one of these things, they're going to read the constructor documentation. Mentioning it there would be fine. It's not like the people you're dealing with are malicious.

#4868891 HTML5

Posted by Slavik81 on 04 October 2011 - 02:03 AM


I haven't read it myself, but got it recommended a year back by some IT journalist.

That is fascinating.

#4867858 Crazy C++/VS Crash - Deleted my file!

Posted by Slavik81 on 30 September 2011 - 09:56 PM

Take a look under Right-click->Properties->Previous Versions. And pray that your computer has a backup.

I'd recommend investing some time in setting up version control for next time.

#4862167 TopLevel Game Develop Company's test questions

Posted by Slavik81 on 15 September 2011 - 12:58 PM

Assuming that two trials with the same box are independent, the answer depends on the probability distribution of the box breaking on any given floor. The strategy you use, depends on what you expect the distribution to be, and how much you care about minimizing the worst case vs. the average case.

I kind of hate these sorts of questions. Even with a ruggedized Xbox, I'd expect it to break within the first 3 floors. That expectation would change the expected optimal algorithm, presuming you cared about minimizing the average case, rather than minimizing the worst case. Stating the question more abstractly would improve their question, given that they're clearly looking for a specific answer.

When you ask a real-world question, unless you're careful, you get real-world baggage that comes with it. For a further example, you might drop both Xboxes from the same floor and have one survive and one break. There's a reason why actual scientific testing uses multiple trials and statistical analysis of the results.

A better way to phase the question would be:
"Given a random natural number y, find the maximum value of x between 0 and 100 for which x <= y. However, you may only use the test x <= y and it may only return false twice over the course of your algorithm. Optimize your algorithm to minimize the worst case number of comparisons and state what the worst case is."

#4860603 Looking for some "best practices" advice

Posted by Slavik81 on 12 September 2011 - 02:57 AM

Effective C++ by Scott Meyers has good advice about language-specific coding details.

#4853470 Component entities: Actually doing it! C++ style

Posted by Slavik81 on 24 August 2011 - 07:09 PM

My personal writeup has some additional information, but not much more than can be gleaned from the slides. I don't know if the full video of the session is available on GDC Vault or not, but if it is, it's well worth the price of admission.

I found the audio for $3.95