• Hey, guest user. Hope you're enjoying NeoGAF! Have you considered registering for an account? Come join us and add your take to the daily discourse.

Programming |OT| C is better than C++! No, C++ is better than C

missile

Member
... my next question is, are the concepts of matrix's, vectors, linear algebra, and quarternions covered under discrete math, something else, or are they part of their own discipline? I'm interested in going into game programming and possibly specializing in graphics engine programming among other things and heard these concepts were key, as well as a background in discrete math. If I'm aiming for this what else would I need? And is a graphics engine programmer a good field to specialize in if I wanted to go into game programming? Would the concepts required to become adept in the field be easily translatable to other programming disciplines if that job prospect ultimately doesn't follow through?
Becoming a graphics head...
 

usea

Member
To me the number of spaces in your tabs is a standard that is up for personal preference.
He means if you use spaces you should have a standard. It's insane to constantly change the number of spaces in the code your coworkers commit. If you use spaces, it's critical that everybody uses the same number, whatever that is.
 

Omikron

Member
He means if you use spaces you should have a standard. It's insane to constantly change the number of spaces in the code your coworkers commit. If you use spaces, it's critical that everybody uses the same number, whatever that is.

True, my mistake.
 
Sorry to interrupt the spaces vs. tabs war but...

I am only taking one class next summer session and really want to become more skilled in C++. I was thinking of taking an old open source project or game like Aleph One and spend five weeks understanding advanced coding practices and large project structure.

Is this a good idea or is there a better way to learn what I'm thinking about (i.e. advance C++ skills, large structure program)?
 

diaspora

Member
Erm, so in Learn C the Hard Way, I'm a bit confused. What purpose does the '\0' serve in this? When I compiled and ran it with it in there I got "Zed A. Shaw", but with a bunch of miscellaneous characters included. When I removed the '\0' it came out just fine. Is this one of those UNIX vs Windows oddities?

Code:
#include<stdio.h>

	int main(int argc, char *argv[])
	{
		int areas[] = {10, 12, 13, 14, 20};
		char name[] = "Zed";
		char full_name[] = {
			'Z', 'e', 'd',
			' ', 'A', '.', ' ',
			'S', 'h', 'a', 'w', '/0'
		};
		
		printf("name=\"%s\" and full_name=\"%s\"\n", name, full_name);
		
		return 0;
	}
 
Hey Neogaf, is there a way to multiple two numbers without using the "*" symbol in JAVA?

I need to learn JAVA well; I learned it before, but I need to strengthen my skills. Does anyone know of a good source for JAVA interview questions, writing some functions and OOP concepts? Sorry for asking, but I am tired of living at home and I want to start my life. If not, then I'll look for myself.
 

tokkun

Member
Erm, so in Learn C the Hard Way, I'm a bit confused. What purpose does the '\0' serve in this? When I compiled and ran it with it in there I got "Zed A. Shaw", but with a bunch of miscellaneous characters included. When I removed the '\0' it came out just fine. Is this one of those UNIX vs Windows oddities?

Code:
#include<stdio.h>

	int main(int argc, char *argv[])
	{
		int areas[] = {10, 12, 13, 14, 20};
		char name[] = "Zed";
		char full_name[] = {
			'Z', 'e', 'd',
			' ', 'A', '.', ' ',
			'S', 'h', 'a', 'w', '/0'
		};
		
		printf("name=\"%s\" and full_name=\"%s\"\n", name, full_name);
		
		return 0;
	}

Did you use '\0' as it says in your text or '/0' as it says in your code?

'\0' is the value zero, also known as the null character. It indicates the end of a string. Without the null char, the program would not know when to stop printing. Therefore printing a string without a null char is considered unsafe.

The reason it worked properly without the character is a coincidence; memory regions are often initialized to zero. It happened that the next character was a null character even though you didn't set it.
 

hateradio

The Most Dangerous Yes Man
Hey Neogaf, is there a way to multiple two numbers without using the "*" symbol in JAVA?
If you're using BigInteger or some other class you'd do myBigInt.multyply(otherBigInt). I don't know if there's a way to do that with normal Integers or why you'd want to.
 
Hey Neogaf, is there a way to multiple two numbers without using the "*" symbol in JAVA?

I need to learn JAVA well; I learned it before, but I need to strengthen my skills. Does anyone know of a good source for JAVA interview questions, writing some functions and OOP concepts? Sorry for asking, but I am tired of living at home and I want to start my life. If not, then I'll look for myself.

If you're dealing with integers, you can simply add. For example, 4 * 3 is a loop of 3 iterations adding 4. Then deal with any possible signs.
 

Ixian

Member
I need to learn JAVA well; I learned it before, but I need to strengthen my skills. Does anyone know of a good source for JAVA interview questions, writing some functions and OOP concepts? Sorry for asking, but I am tired of living at home and I want to start my life. If not, then I'll look for myself.
I recommend reading Coding Interviews and Programming Interviews Exposed. I've also read Cracking the Coding Interview, but prefer the other two.

In other programming-related news for me, I quit my old job last month and am starting a new job in the games industry later this month. So excited!
 

Ixian

Member
Congrats! How did you prepare? Or were you already working in a field that uses computer graphics?
I'm actually doing more generalized programming; there was a graphics position open but I know I'm not strong enough in that area to perform well.

As for how I prepared, I was actually a developer at my old job as well, but I started there as a software test engineer and transitioned to a developer role over time without undergoing an interview process. So, despite my experience, I was incredibly nervous about the interview! Programming is such a vast topic and it's easy to get dinged on something you just haven't used in a while, especially if you tend to make use of already existing libraries in your day-to-day work. I spent the week leading up to the interview reviewing pretty much the entirety of C++, up to the moment the interview started. It felt like cramming for an exam back in college. :D
 

tokkun

Member
Programming interviews are pretty awful as a whole. They are really only good at eliminating false positives in the hiring process, but can create a lot of false negatives.

Whiteboarding is just not representative of the actual process of being a coder, and beyond some conceptual stuff, I am more interested in whether the person can find the right answer in a timely manner rather than caring that they have it memorized - particularly in the case of language or library features.
 

Ixian

Member
I was actually having a discussion about interviews with some of the developers that were a part of the "loop" process at my previous company. In particular, they mentioned one guy who pretty much bombed every technical part of the interview but one of the devs took him out to lunch and talked with him there, and liked him enough to have him brought on board. He ended up being one of the better developers.
 

usea

Member
Guys, please hold me? I'm going to cry.

These getters are changing object state. I don't know if I can handle this.
:(

Speaking of getters, that reminds me. Earlier this week I found a bug in some code I wrote recently. The class looked something like this

Code:
public class MotionRule
    {
        public Address Address { get { return this.Address; } }
        private Address _address;        
        [...]
    }
}
The typo might be obvious if you're familiar with C#. My getter returns itself rather than the private member. It's not common for me to explicitly back my properties with private members, but I was copying the style in this particular project and made a typo. Anyway, the compiler doesn't catch that error and whoops infinite loop.

Here's another bug I introduced last week:
Code:
public class RecordSaver
{
    public void Save<T>(T record)
    {
        Save(new []{ record });
    }

    public void Save<T>(ICollection<T> records)
    {
        //save records to disk
    }
}
This class just saves records to a database. I provided an overload in case you want to save one record or many records, just to make the API slightly friendlier. But oops, the type parameter T is not on the class, it's on the methods. T isn't necessarily the same T in the two methods. So unless you're saving an ICollection<T> exactly, it will call the first method. Not only that, but since the first method is trying to call the second with an array, it will actually just call itself. This usually results in an out of memory exception before a stack overflow. But yeah, bad news.

Whoops!
 

Sharp

Member
Guys, please hold me? I'm going to cry.

These getters are changing object state. I don't know if I can handle this.
Lots of getters change object state, e.g. getters that cache. You'll have to be more specific for me to feel sorry for you.

Who the hell uses tabs for spacing other than .NET developers?
 
So is anyone in this community good with concurrency? I'm starting to rewrite a large module for our back end software and I think it could be really better optimized for modern processors by parallelizing it, but I am hesitant because of the complexity of concurrency (and, obviously, my deadline).

Basically it's going to read geopositional messages off of a socket (or sockets), parse the messages, write them to a database, and then I have to decide whether I want to do further analysis on them in the database or in the server application itself.

As it is right now it dedicates a thread to each socket to read the messages in, then places all of the messages in a single queue, which a single thread then uses to parse. The messages are very small, less than (82 character maximum per sentence, multi-sentence messages are possible but relatively rare).

Once they are parsed they are placed into a single queue for a single thread to write them to a database. They are persisted in memory and two separate threads perform additional analysis on them.

So it's already sort of a hairy mess, but it's largely producer/consumer relationships. There are several places where I think these relationships can be changed from single threads to multiple threads to improve performance on modern architectures.

The raw messages that are read off of the socket are parsed by a single thread. The current problem I have is that the class used to parse the messages was set up to use a single static member containing the message bits to do the parsing ops on, so I have been rewriting it so that it is actually thread safe. My plan is to use a thread pool to handle parsing tasks, so that multiple messages can be parsed simultaneously. My conflict is that I am not sure the parse task is complex enough that multiple threads are even necessary, but I would imagine I can just reduce the thread pool size if that's an issue, that way it can scale up in the future if necessary.

The next obvious bottleneck as far as I can tell is where it is written to the database. The database writes are done on a single thread that constantly blocks on I/O with each write transaction. I plan to create another thread pool here so that multiple threads are executing the writes to improve throughput, since it won't have issues with writes being blocked on I/O as frequently.

As for the actual geospatial analysis, I think I am going to move the entire thing into the database and use SQL Server 2008R2s built in geography functions for it. I'm assuming the algorithms they have for proximity detection and intersections are probably far better than anything I can write. I just worry about about the database contention it could cause.

Any tips or suggestions for the situation? I have bought and I am reading Java Concurrency in Practice by Brian Goetz as I have basically gathered he is the foremost expert on concurrency. Any pitfalls anyone has experience that I need to look out for?
 

usea

Member
Are you in Java or .NET? Either way it sounds like you have a pretty good handle on it. In .NET I don't use the older stuff like thread pools anymore, I prefer using the Task Parallel Library.

There are lots of ways to make classes thread safe. Some of them will perform better than others. Any time you can avoid keeping state at all, the better off you'll be. Is it possible you could do your parsing without saving any state at all between calls? Otherwise you'll probably end up with synchronization which is going to slow stuff down (though not necessarily by a lot). Sorry if you know all this already.

Also, it sounds like if you do everything right, then the database will be the bottleneck. Slowing that bottleneck by doing geospatial analysis in the database might not be the way to go. My coworker just finished a project using the geospatial stuff in sql server 2008 r2, but I don't know how much work he left up to sql server and how much he rolled on his own.
 

Sharp

Member
So is anyone in this community good with concurrency? I'm starting to rewrite a large module for our back end software and I think it could be really better optimized for modern processors by parallelizing it, but I am hesitant because of the complexity of concurrency (and, obviously, my deadline).

Basically it's going to read geopositional messages off of a socket (or sockets), parse the messages, write them to a database, and then I have to decide whether I want to do further analysis on them in the database or in the server application itself.

As it is right now it dedicates a thread to each socket to read the messages in, then places all of the messages in a single queue, which a single thread then uses to parse. The messages are very small, less than (82 character maximum per sentence, multi-sentence messages are possible but relatively rare).

Once they are parsed they are placed into a single queue for a single thread to write them to a database. They are persisted in memory and two separate threads perform additional analysis on them.

So it's already sort of a hairy mess, but it's largely producer/consumer relationships. There are several places where I think these relationships can be changed from single threads to multiple threads to improve performance on modern architectures.

The raw messages that are read off of the socket are parsed by a single thread. The current problem I have is that the class used to parse the messages was set up to use a single static member containing the message bits to do the parsing ops on, so I have been rewriting it so that it is actually thread safe. My plan is to use a thread pool to handle parsing tasks, so that multiple messages can be parsed simultaneously. My conflict is that I am not sure the parse task is complex enough that multiple threads are even necessary, but I would imagine I can just reduce the thread pool size if that's an issue, that way it can scale up in the future if necessary.

The next obvious bottleneck as far as I can tell is where it is written to the database. The database writes are done on a single thread that constantly blocks on I/O with each write transaction. I plan to create another thread pool here so that multiple threads are executing the writes to improve throughput, since it won't have issues with writes being blocked on I/O as frequently.

As for the actual geospatial analysis, I think I am going to move the entire thing into the database and use SQL Server 2008R2s built in geography functions for it. I'm assuming the algorithms they have for proximity detection and intersections are probably far better than anything I can write. I just worry about about the database contention it could cause.

Any tips or suggestions for the situation? I have bought and I am reading Java Concurrency in Practice by Brian Goetz as I have basically gathered he is the foremost expert on concurrency. Any pitfalls anyone has experience that I need to look out for?
Couple things: one thread per socket isn't necessarily the most optimal way of doing things, since it results in a bunch of blocking reads. You'd be better off single threading it and using epoll (or whatever the Windows equivalent is). Secondly, absent asynchronous IO (which databases do tend to take advantage of) you may not save as much as you think multithreading the writes.

You are probably right that the database can analyze the data better than you can, but the advantage of doing that outside the database is that you can do it on its own server, rather than stealing resources from the database server.

Multithreading the parsing could be a good idea depending, as you said, on the complexity of the parse. Is it greater than the cost of the lock/unlock?
 
Are you in Java or .NET? Either way it sounds like you have a pretty good handle on it. In .NET I don't use the older stuff like thread pools anymore, I prefer using the Task Parallel Library.

There are lots of ways to make classes thread safe. Some of them will perform better than others. Any time you can avoid keeping state at all, the better off you'll be. Is it possible you could do your parsing without saving any state at all between calls? Otherwise you'll probably end up with synchronization which is going to slow stuff down (though not necessarily by a lot). Sorry if you know all this already.

Also, it sounds like if you do everything right, then the database will be the bottleneck. Slowing that bottleneck by doing geospatial analysis in the database might not be the way to go. My coworker just finished a project using the geospatial stuff in sql server 2008 r2, but I don't know how much work he left up to sql server and how much he rolled on his own.

Java.

Yeah, I am currently rewriting the parser to use only local members and just pass them to the utility methods. Before it was using a class member, and instead of passing them the utility methods of the class all operated on that one class member. I didn't like it, but I sort of understand why it was done. In theory clearing that one class member may give better performance than initializing a new one each time.

As far as the Task Parallel Library goes, I am not super familiar with C#, but from what I remember that is more useful in divide and conquer scenarios. I think in my scenario it's more of a pure producer/consumer issue, and I just need to up the number of consumers I have. It's going to run on a server with 8 cores and the entire module consists of maybe 4 threads as it stands right now.

As far as the database contention goes... I guess it's sort of wait and see. I might just have to try both ways and see which performs better.
 
Programming interviews are pretty awful as a whole. They are really only good at eliminating false positives in the hiring process, but can create a lot of false negatives.

Whiteboarding is just not representative of the actual process of being a coder, and beyond some conceptual stuff, I am more interested in whether the person can find the right answer in a timely manner rather than caring that they have it memorized - particularly in the case of language or library features.

I was tasked with doing the 1st round of interviews at my company last year for several positions we had open. I did the interviews in the same style of the companies I wanted to work for when I graduated, which was to ask algorithm questions on a whiteboard. This approach eliminated 1 candidate (out of 15) who truly didn't know a for loop from a rock. All the other candidates could have learned the skills on the job because they all had the fundamentals down, and it doesn't take a genius to work on .NET desktop GUI applications.

Needless to say, this approach didn't help me at all to "truly" differentiate the great candidates. Furthermore, I gained some insight a few months ago, that once you start interviewing experienced developers, these type of academic questions are horrible and pointless.
 

usea

Member
You're mostly right about the TPL. But it also provides a thread abstraction called Task which uses the thread pool via a scheduler. Instead of manually making threads and trying to tune it to whatever machine you're running on, you just make a bunch of Tasks and the scheduler multiplexes them to threads based on the performance at runtime. They're like tiny lightweight threads. Also they're just easier to use than a thread.

Not so much for the performance gain as for the ease of use.
 
You're mostly right about the TPL. But it also provides a thread abstraction called Task which uses the thread pool via a scheduler. Instead of manually making threads and trying to tune it to whatever machine you're running on, you just make a bunch of Tasks and the scheduler multiplexes them to threads based on the performance at runtime. They're like tiny lightweight threads. Also they're just easier to use than a thread.

Not so much for the performance gain as for the ease of use.

That actually sounds a lot like ThreadPoolExecutor in java. You create it with a core pool of threads, and a max number of threads, and a queue to hold tasks (basically just a queue of overridden Runnables) and it will dynamically allocate threads to run them based on need and the policy which you set.

Couple things: one thread per socket isn't necessarily the most optimal way of doing things, since it results in a bunch of blocking reads. You'd be better off single threading it and using epoll (or whatever the Windows equivalent is). Secondly, absent asynchronous IO (which databases do tend to take advantage of) you may not save as much as you think multithreading the writes.

As far as the socket goes, it gets something around 50,000 messages per minute, per socket. It's basically constantly reading.
 
Hi all I've done some work with Java (writing code for some selenium automation) and currently work with PhP daily (modifying existing code) I'm looking to learn C++ just for personal enjoyment because I liked my time spent coding the little bit of Java I did code. This would also give me more options for future jobs which is always good i suppose.

Now that thats all out of the way my question is. Is this book still relevant?

Programming: Principles and Practice Using C++ by Bjarne Stroustrup

I ask because it was published in 2008 and I wouldn't want to study outdated material. I think its cool that I could read a book written by the C++ man himself. Any other recommendations for a beginner are greatly appreciated.
 

A Human Becoming

More than a Member
I considered myself a somewhat intelligent person. Not genius, but capable. When I said Coursera was offering a class on linear algebra using Python (Coding the Matrix: Linear Algebra through Computer Science Applications), I thought sure, I did well in math during high school (well, until Calculus) and I know some Python.

This is me watching the first lecture video:
homer-brain-leave-o.gif
 

Sharp

Member
That actually sounds a lot like ThreadPoolExecutor in java. You create it with a core pool of threads, and a max number of threads, and a queue to hold tasks (basically just a queue of overridden Runnables) and it will dynamically allocate threads to run them based on need and the policy which you set.



As far as the socket goes, it gets something around 50,000 messages per minute, per socket. It's basically constantly reading.
Exactly--you end up wasting a lot of unnecessary time switching between threads. At a hardware level, information coming through the network generally comes in one message at a time, so it can't really be multithreaded there. Keep in mind that if you're using TCP, receiving a message also involves writing information back to the sender, which is again a blocking operation. As a result, when you do blocking reads in multiple threads reading from multiple sockets, you have a situation where the actual expensive operation (calling out to the kernel to read from the socket) includes several steps that can only happen when nobody else is reading (this can vary based on kernel implementation). That's why single-threaded servers like nginx can usually handle far more simultaneous connections than thread-per-socket ones like Apache; most of the threads are sleeping at any one time. Additionally, since the polling blocking reads do to find out if your socket is used can be O(n) in the number of open sockets, it's more efficient to do it as one call (e.g. to epoll).

(The above is all true of Linux. Windows may work differently but I think it's likely similar).

Long story short is, concurrent programming can be tricky when you're interacting with inherently single-threaded systems.
 
Exactly--you end up wasting a lot of unnecessary time switching between threads. At a hardware level, information coming through the network generally comes in one message at a time, so it can't really be multithreaded there. Keep in mind that if you're using TCP, receiving a message also involves writing information back to the sender, which is again a blocking operation. As a result, when you do blocking reads in multiple threads reading from multiple sockets, you have a situation where the actual expensive operation (calling out to the kernel to read from the socket) includes several steps that can only happen when nobody else is reading (this can vary based on kernel implementation). That's why single-threaded servers like nginx can usually handle far more simultaneous connections than thread-per-socket ones like Apache; most of the threads are sleeping at any one time. Additionally, since the polling blocking reads do to find out if your socket is used can be O(n) in the number of open sockets, it's more efficient to do it as one call (e.g. to epoll).

(The above is all true of Linux. Windows may work differently but I think it's likely similar).

Long story short is, concurrent programming can be tricky when you're interacting with inherently single-threaded systems.

That's interesting. I think I'll have to do some more research on that subject.
 
So when I need to add something to a Listbox how do I continue have it being inserted? Say that everytime I click a button I want it to add "Hello" in the Listbox. And everytime I click on the button more and more "hello"s show up. How do I do that? Because with what I currently am doing the "Hello" shows up once and that's it, nothing else gets added.

Here's what I have:

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Do Until ListBox1.Items.Count = 1
ListBox1.Items.Add("Hello")
Loop
End Sub
End Class
 

usea

Member
So when I need to add something to a Listbox how do I continue have it being inserted? Say that everytime I click a button I want it to add "Hello" in the Listbox. And everytime I click on the button more and more "hello"s show up. How do I do that? Because with what I currently am doing the "Hello" shows up once and that's it, nothing else gets added.

Here's what I have:

Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Do Until ListBox1.Items.Count = 1
            ListBox1.Items.Add("Hello")
        Loop
    End Sub
End Class
Surround your code with "code" tags so it shows up correctly. There's a button in the UI for it now. :)

I don't know VB, but it looks like your code says "add Hello to ListBox1 until the listbox has 1 item in it." So it will never add more than 1 thing.

If you remove the loop completely it will add "Hello" every time you click.
 
Programming interviews are pretty awful as a whole. They are really only good at eliminating false positives in the hiring process, but can create a lot of false negatives.

Whiteboarding is just not representative of the actual process of being a coder, and beyond some conceptual stuff, I am more interested in whether the person can find the right answer in a timely manner rather than caring that they have it memorized - particularly in the case of language or library features.
Felt the same way about programming exams (IS, not CS major in school, so I only took one programming class, but still). They don't represent the situation in which you actually code.
I considered myself a somewhat intelligent person. Not genius, but capable. When I said Coursera was offering a class on linear algebra using Python (Coding the Matrix: Linear Algebra through Computer Science Applications), I thought sure, I did well in math during high school (well, until Calculus) and I know some Python.

This is me watching the first lecture video:
homer-brain-leave-o.gif

lol perfect
 

Aleph

Member
Hi all I've done some work with Java (writing code for some selenium automation) and currently work with PhP daily (modifying existing code) I'm looking to learn C++ just for personal enjoyment because I liked my time spent coding the little bit of Java I did code. This would also give me more options for future jobs which is always good i suppose.

Now that thats all out of the way my question is. Is this book still relevant?

Programming: Principles and Practice Using C++ by Bjarne Stroustrup

I ask because it was published in 2008 and I wouldn't want to study outdated material. I think its cool that I could read a book written by the C++ man himself. Any other recommendations for a beginner are greatly appreciated.

I have that book, and this year's reference book (covers C++11), I'd say it's good for learning the basics of the language, as well as some techniques like RAII, and how to use some STL containers (e.g, unordered_map, unordered_set - which are actually a part of the latest standard). Unfortunately it doesn't cover some C++11 stuff, like move operations, r-value references, the "auto" keyword, and it also mentions the auto_ptr class, which is now deprecated (in favour of unique_ptr, etc.), but I guess those could be learnt from other sources.
 
Anyone ever make composite controls in ASP.Net? I'm extending a GridView to have a checkbox column, but I'm having issues with the ID name changing the first time it loads. After that it works fine.

I'm not sure where to put the creation of checkboxes so that I don't deal with this problem, right now it's created dynamically in the OnRowCreated state...

i'm trying to solve it without any client side javascript because it's a composite control and i want users to be able to just add a reference to the dll, create a toolbox, and add the Custom control.
 
Sorry to interrupt the spaces vs. tabs war but...

I am only taking one class next summer session and really want to become more skilled in C++. I was thinking of taking an old open source project or game like Aleph One and spend five weeks understanding advanced coding practices and large project structure.

Is this a good idea or is there a better way to learn what I'm thinking about (i.e. advance C++ skills, large structure program)?

Anybody? I've read some stuff in the op but I just want to make sure I'm not wasting my time.
 
Anybody? I've read some stuff in the op but I just want to make sure I'm not wasting my time.

Two suggestions. First, http://see.stanford.edu/see/courseinfo.aspx?coll=11f4f422-5670-4b4c-889c-008262e09e4e This course did a good job helping me understand a good number of data structures/algorithms using C++. Caveat: the course requires you to use their library which plays a little nicer than the STL.

Second, if you are considering looking into large projects you may try one of the code reviews here: http://fabiensanglard.net/
 

Slavik81

Member
Sorry to interrupt the spaces vs. tabs war but...

I am only taking one class next summer session and really want to become more skilled in C++. I was thinking of taking an old open source project or game like Aleph One and spend five weeks understanding advanced coding practices and large project structure.

Is this a good idea or is there a better way to learn what I'm thinking about (i.e. advance C++ skills, large structure program)?
That sounds like a good idea to me. Add features to it. Start small and slowly get bigger.

Though, as warning: real world software projects are usually not uniformly good examples of design. When I first started working on my first big software project, I assumed that the people who developed it knew more than me, and thus that I should emulate patterns I found in that software.

Long story short, that was wrong. The people who built that software did not know what they were doing, and many of the things I 'learned' were garbage. The silver lining being that it helped me to understand what makes bad software bad software.
 
I considered myself a somewhat intelligent person. Not genius, but capable. When I said Coursera was offering a class on linear algebra using Python (Coding the Matrix: Linear Algebra through Computer Science Applications), I thought sure, I did well in math during high school (well, until Calculus) and I know some Python.

This is me watching the first lecture video:
homer-brain-leave-o.gif

Perfection..

My broken math foundation was one of the reason i wanted to write an software renderer as an minor project. If you have 18 weeks for it with other classes its dive into the deep hole and climb out of it before the bloody deadline.

Now im planning on doing some research into animations and maybe some motion capture stuff for next minor project. Crawl the internet and fill up my bookmarks with links and book source to get before uni starts again. And hopefully spend more time on working on the project and start making some documentation in my summer stop.

And im going to work through some c++, concurrent and hardware for programming stuff and some math like algebra and calculus books the next 6~12 months.
 

Randdalf

Member
Given a templated C++ class such as

Code:
template<class T>
class Interface
{
    bool operator==(const T &rhs) = 0;
}

I am going to use Interface to enforce a set of operator functions that must be implemented by a subclass of Interface, using:

Code:
class Entity : Interface<Entity>
{
    Entity(int i);
    bool operator==(const Entity &rhs);
}

The problem I'm having is that I want to then create a templated Container class with the restriction that Container can only use classes derived from Interface as part of its template, i.e. I want to be able to write the following code

Code:
int main()
{
    Entity e(23);
    Container<Entity> ctr;
    int number = 23;

    ctr.add(e); // fine, because e is a subclass of Interface
    ctr.add(number); // not fine, because number is not a subclass of Interface
}

In other words, I want Container to look something like
Code:
template<Interface T> // this doesn't work
class Container
{
    void add(const T &item);
}

But I don't know how to correctly have the argument of a template... be a template, without instantiating its argument. Is this possible?

EDIT:
Code:
template<template<class> Interface T>
class Container
{
    void add(const T &item);
}

Doesn't seem to work. Is there a better way of doing this?
 

Sharp

Member
Given a templated C++ class such as

Code:
template<class T>
class Interface
{
    bool operator==(const T &rhs) = 0;
}

I am going to use Interface to enforce a set of operator functions that must be implemented by a subclass of Interface, using:

Code:
class Entity : Interface<Entity>
{
    Entity(int i);
    bool operator==(const Entity &rhs);
}

The problem I'm having is that I want to then create a templated Container class with the restriction that Container can only use classes derived from Interface as part of its template, i.e. I want to be able to write the following code

Code:
int main()
{
    Entity e(23);
    Container<Entity> ctr;
    int number = 23;

    ctr.add(e); // fine, because e is a subclass of Interface
    ctr.add(number); // not fine, because number is not a subclass of Interface
}

In other words, I want Container to look something like
Code:
template<Interface T> // this doesn't work
class Container
{
    void add(const T &item);
}

But I don't know how to correctly have the argument of a template... be a template, without instantiating its argument. Is this possible?

EDIT:
Code:
template<template<class> Interface T>
class Container
{
    void add(const T &item);
}

Doesn't seem to work. Is there a better way of doing this?
Rather than explicitly restricting to subclasses of Interface<T>, have you tried doing it like this?

Code:
template<class T>
class Container
{
    void add(const Interface<T> &item);
}
 

Randdalf

Member
Rather than explicitly restricting to subclasses of Interface<T>, have you tried doing it like this?

Code:
template<class T>
class Container
{
    void add(const Interface<T> &item);
}

Ah, that's a good solution, thanks, just implemented it. Is there any way of ensuring that inserted objects are of type T in the add function itself? I say this because you could, for instance, create a class Evil : Interface<Good> and add it into a Container<Good>, even though it's not of type Good. There is also the problem of casting to items of type T inside the Container, which could be potentially expensive.
 

tokkun

Member
Ah, that's a good solution, thanks, just implemented it. Is there any way of ensuring that inserted objects are of type T in the add function itself? I say this because you could, for instance, create a class Evil : Interface<Good> and add it into a Container<Good>, even though it's not of type Good. There is also the problem of casting to items of type T inside the Container, which could be potentially expensive.

This....doesn't make sense. If Evil : Interface<Good>, then it will be specialized to use type Good. Interface is an abstract class, so the only way you will be able to get any objects that you can put in Container<Good> is to create non-abstract subclasses, like Evil.

Also, just as general advice, if you find yourself doing typecasting of one of the templated types in a template class, you may want to stop to consider whether what you are doing is a good design, because in most cases it probably is not.

Are you sure you need to use templates and an interface class to do what you want to do here? You can make an object with an equality operator without requiring a template. You can also store such objects in a templated container that uses the equality operator.

If you are worried about someone trying to store an object without an equality operator in the container, that will generate a compile-time error.
 

Randdalf

Member
This....doesn't make sense. If Evil : Interface<Good>, then it will be specialized to use type Good. Interface is an abstract class, so the only way you will be able to get any objects that you can put in Container<Good> is to create non-abstract subclasses, like Evil.

Also, just as general advice, if you find yourself doing typecasting of one of the templated types in a template class, you may want to stop to consider whether what you are doing is a good design, because in most cases it probably is not.

Are you sure you need to use templates and an interface class to do what you want to do here? You can make an object with an equality operator without requiring a template. You can also store such objects in a templated container that uses the equality operator.

If you are worried about someone trying to store an object without an equality operator in the container, that will generate a compile-time error.

Yeah, I guess that's the key point here, I tend to like to formalise these things a bit too much. I'll go with a simpler solution instead.
 
Anybody here good with OpenGL? Can you please help?

I have a function that draw 3D object with texture and it work.

Code:
void drawTexturedShape(int img) {
    glColor3f(0.8, 0.8, 0.8);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, textures[img]);
    glBegin(GL_QUADS);
    	...
    glEnd();
    glDisable(GL_TEXTURE_2D);
}

I have created a test function to draw a 2D shape.

Code:
void draw2DShape() {
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
        glVertex2f(-20, -20);
        glVertex2f(-20,  20);
        glVertex2f(20,   20);
        glVertex2f(20,  -20);
    glEnd();
    glFlush();
}

When I call the draw2DShape() function. It doesn't show up.

Code:
void RenderTheScene(void) {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    
    // Draw a 2D shape
    draw2DShape();
    
    // Draw a 3D shape
    glPushMatrix();
    glTranslatef(-7.0, -18.0, 0.0);
    glScalef(5.4, 0.8, 3.0);
    drawTexturedShape(IMAGE8);
    glPopMatrix();
}

Why is that? It is really frustrating.
 
Are you sure it's not a backface culling problem? glDisable(GL_CULL_FACE)
Does it work with untextured quads?
Does it work with triangles?
 
Top Bottom