c++

Difference between operator new and operator new[]?

I've overloaded the global operator new/delete/new[]/delete[] but simple tests show that while my versions of new and delete are being called correctly, doing simple array allocations and deletes with new[] and delete[] causes the implementations in newaop.cpp and delete2.cpp to be called. For example, this code int* a = new int[10]; ...

Hiding private constants in an inline namespace header

I have some inline functions contained within a namespace in a header file and am not currently in a position to move them into a cpp file. Some of these inline functions use magic constants, for example: // Foo.h namespace Foo { const int BAR = 1234; inline void someFunc() { // Do something with BAR } } Howe...

Virtual tables on anonymous classes

I have something similar to this in my code: #include <iostream> #include <cstdlib> struct Base { virtual int Virtual() = 0; }; struct Child { struct : public Base { virtual int Virtual() { return 1; } } First; struct : public Base { virtual int Virtual() { return 2; } } Second; }; int main() { Child child; ...

Object oriented programming , inheritance , copy constructors

Suppose I have a base class Person and I publicly inherit a class Teacher from base class Person. Now in the main function I write something like this // name will be passed to the base class constructor and 17 // is for derived class constructor. Teacher object(“name”,17) ; Teacher object1=object; //call to copy constructor Now ...

How do I read a given number of lines from the end of the file using streams in C++?

For my implementation of tail shell command in Linux, I need to read certain amount of lines/bytes from the end of the file using stream input/output. Does anyone have suggestions how to do that? I suspect I need to open a file and pass some parameter to the ifstream constructor, but I don't know what exactly. Googling didn't find anythi...

C++: Visibility of parent namespace to child namespace

If you have ns2 as a child namespace of ns1, and you use ns1 classes inside a header for an ns2 class, do you need to explicitly decalre it as you would when ns1 & ns2 are unrelated? e.g button.h namespace ns1 { class Button { ... }; } dialog.h include "button.h" namespace ns1 { namespace ns2 { class TestDialog { Butt...

[C++] Ofstream writing the wrong thing into a file

Hey guys, I couldn't really think of what to call this error in the title, so here goes. I'm starting an assignment where I have to read the contents of a file, perform some calculations and write the contents + the new calculations to a file. I wrote the code to read in the file, and to right away write it into an output file to tes...

C++ - Variable "not" declared

Hello everyone, this is my first time on stack overflow so please be nice to me =). Right now, I have a problem with my code, while developing, I have encountered this a million times, and managed to solve it. But right now, I'm writing some serious code, and was wondering. Why does its happens? What's the correct way to fix it up T...

Optimizing for space instead of speed in C++

When you say "optimization", people tend to think "speed". But what about embedded systems where speed isn't all that critical, but memory is a major constraint? What are some guidelines, techniques, and tricks that can be used for shaving off those extra kilobytes in ROM and RAM? How does one "profile" code to see where the memory bloat...

Array as private member of class.

I am trying to create a class which has a private member that is an array. I do not know the size of the array and will not until the value is passed into the constructor. What is the best way to go about defining the class constructor as well as the definition in the .h file to allow for this variable size of the array? ...

What could C/C++ "lose" if they defined a standard ABI?

The title says everything. I am talking about C/C++ specifically, because both consider this as "implementation issue". I think, defining a standard interface can ease building a module system on top of it, and many other good things. What could C/C++ "lose" if they defined a standard ABI? ...

What will this construction do?

std::vector<int> a; int p; int N; // ... p = a[ N>>1 ]; What is the N>>1 part? ...

visual studio 2008 isn't creating an .exe file when i build my project. any ideas why?

i'm new to visual studio and couldn't find anything on google about this. i know this is an extremely noobish question, but i can't seem to find any info for it. the debug shows me whatever i write, and the build has no errors, so i know the code i'm writing is fine. the release folder doesn't contain the .exe, even after i build it, r...

Why doesn't std::istream assume ownership over its streambuf?

Hi, I am writing some sort of virtual file system library for video-games in the likes of CRI Middleware's ROFS (see Wikipedia). My intention with the library is to provide natural means of accessing the resources of the games I develop, which store some data embedded in the executable, some on the media and some on the local user's har...

Optimal method to create a large string containing several variables?

I want to create a string that contains many variables: std::string name1 = "Frank"; std::string name2 = "Joe"; std::string name3 = "Nancy"; std::string name4 = "Sherlock"; std::string sentence; sentence = name1 + " and " + name2 + " sat down with " + name3; sentence += " to play cards, while " + name4 + " played the violin."; Thi...

Experience with Association Rules and Frequent Itemsets

Has someone ever used some of these Algorithms ? What for ? ...

Reading text files

I'm trying to find out what is the best way to read large text (at least 5 mb) files in C++, considering speed and efficiency. Any preferred class or function to use and why? By the way, I'm running on specifically on UNIX environment. ...

STL containers element destruction order

Does ISO C++ standard mandate any sort of destruction order of objects inside STL containers? Are std::list/std::vector/std::map elements destroyed starting from the beginning or the end of the container? Can I rely on std::map storing its elements in std::pairs internally so a key in a pair is destroyed before its value (or vice versa...

why is resource management order important?

In Stroustrup's book The C++ Programming language, He mentions "It is typically important that resources are released in the reverse order of their acquisition" why the order is important? void acquire() { acquire resource 1; ... acquire resource n; use resources; release resource n; ... release resource 1; } so what if we cha...

Should I use a class of functions or a namespace of functions?

Say I want some functions to deal with some file, and I was considering 2 options. 1) Create a class like SavedDataHandler that a user could use like this.... // Note that SavedDataHandler has no members. It just has functions that operate on a // resource ( the file) SavedDataHandler gameSave; gameSave.SaveData( arg1, arg2 ); // to sa...