C++ quiz

Ok. This one is not as easy as the first one. (*)

I have the following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
using namespace std;

#include "secret.h"

void swap(long a, long b)
{
    long temp;

    temp=a;
    a=b;
    b=temp;
}

int main()
{
    long a=1;
    long b=2;

    cout << a << ' ';
    cout << b << endl;

    swap(a,b);

    cout << a << ' ';
    cout << b << endl;

    cin.get();
    return 0;
}

The output of this code is:

1 2
2 1

And it actually does the swapping, it's not just some output trick, i.e. it works for every pair of constants I initialize a and b to. So... What's inside my secret header that allows me to do that???

Please, if you find an answer don't post it, PM me instead. Thank you!

(*) http://cplusplus.com/forum/lounge/23263/page7.html#msg125506
Interesting...


I call haxx and output trix on this one.
I'm pretty sure I got it, just sent a PM.
Try again :P
I got to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#ifndef SECRET
#define SECRET

#include <iostream>

#define NULL 0

class ThisClass
{
public:
	ThisClass ( )
		: value ( new int )
	{
	}

	ThisClass ( const ThisClass &o )
		: value ( o.value )
	{
	}

	ThisClass ( int o )
	{
		value = new int;
		*value = o;
	}

	ThisClass &operator = ( ThisClass &o )
	{
		*value = *o.value;
		return *this;
	}

	ThisClass &operator = ( int o )
	{
		*value = o;
	}

	mutable int *value;
};

std::ostream operator << ( std::ostream &strm, ThisClass &cls )
{
	strm << *cls.value;
	return strm;
}

#define long ThisClass

#endif 
Before I started getting errors from private methods/members being accessed. >.>
You are very close! :D Just make it a struct to get rid of the access problems. Oh, and I did it very carefully so as not to have any memory leaks. But if it does the job, I'll accept it, even if it has memory leaks. Oh, and please don't post code! PM me instead.

EDIT: Oh, in fact you got it right... Just do:

std::ostream & operator << ( std::ostream &strm, ThisClass &cls )

and it'll be ok.

Congrats! Now try to do it without memory leaks ;)
Last edited on
*muttering to myself* Damn... I must make these more difficult...
So... What do I win for figuring it out?

Coz, it was... Haxx, right?
AngelHoof wrote:
So... What do I win for figuring it out?

The example you requested in your topic ;)

http://cplusplus.com/forum/beginner/26015/#msg138435
Last edited on
Ok, here's how I did it:

secret.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <map>//???

struct Long
{
    long * ptr;
    static map<long*,long> ref_count; //;D

    Long();
    ~Long();

    Long(long val);
    operator long();

    Long(const Long & L);
    Long & operator=(const Long & L);
};

map<long*,long> Long::ref_count;

Long::Long()
{
    ptr=new long;
    ref_count[ptr]=1;
}

Long::~Long()
{
    if (--ref_count[ptr]==0)
    {
        ref_count.erase(ptr);
        delete ptr;
        ptr=0;
    }
}

Long::Long(long val)
{
    ptr=new long;
    ref_count[ptr]=1;

    *ptr=val;
}

Long::operator long()
{
    return *ptr;
}

Long::Long(const Long & L)
{
    //MWAHAHAHAHAHAHA >:D
    ptr=L.ptr;
    ref_count[ptr]++;
}

Long & Long::operator=(const Long & L)
{
    *ptr=*(L.ptr);
    return *this;
}

//don't forget the magic line ;)
#define long Long 

I guess I should get to work for the next one...
Another (admittedly, very clever) solution, from another site where I also posted the quiz:

secret.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
namespace hidden
{
    void swap(long a, long b);
}

void swap (long & a, long & b)
{
    long temp;

    temp=a;
    a=b;
    b=temp;
}

#define void void hidden:: 
Last edited on
Topic archived. No new replies allowed.