C++98 recap

I needed to run through old C++98 before learning new C++11 features.

code

struct is_even : unary_function<int, bool> {
    bool operator()(int x) const
    {
        return x % 2 == 0;
    }
};

struct buggy_plus : binary_function<int, int, int> {
    int operator()(int a, int b) const
    {
        return a + b + 1;
    }
};

void Hoge::TestFunctionObjects()
{
    // standard function objects
    plus<int> plus;
    minus<double> minus;

    cout << "plus(2,3) = " << plus(2, 3) << endl;
    cout << "minus(2.0f,0.2f) = " << minus(2.0f, 0.2f) << endl;

    // custom unary function objects
    is_even even;
    unary_negate<is_even> odd(even);

    cout << "Is 2 even? " << even(2) << endl;
    cout << "Is 2 odd? " << odd(2) << endl;

    // custom binary function objects
    buggy_plus bplus;
    cout << "bplus(2,3) = " << bplus(2, 3) << endl;

}

void Hoge::TestAlgorithm()
{
    vector<int> v;
    for (int i = 0; i < 10; i++) {
        v.push_back(i);
    }

    // algorithm
    sort(v.begin(), v.end(), greater<int>());
    cout << "---" << endl;
    for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
        cout << *it << endl;
    }

    // remove_if doesn't remove the element, but swap the matched ones to backword and return the last pointer
    vector<int>::iterator last = remove_if(v.begin(), v.end(), bind2nd(less<int>(), 5));

    cout << "---" << endl;
    for (vector<int>::iterator it = v.begin(); it != last; it++) {
        cout << *it << endl;
    }
}

result

* function objects
plus(2,3) = 5
minus(2.0f,0.2f) = 1.8
Is 2 even? 1
Is 2 odd? 0
bplus(2,3) = 6
* algorighm
---
9
8
7
6
5
4
3
2
1
0
---
9
8
7

strong/weak ptr in ARC and C++11

__strong / __weak in ARC vs shared_ptr / weak_ptr in C++11.

result

*** ARC ***
I'm CocoaPitcher, ball = <CocoaBall: 0x7f8809d00570>
I'm CocoaPitcher, ball = (null)
*** C++11 ***
I'm CppPitcher, ball = 7802300.
I'm CppPitcher, ball is null.

main.mm

#import <Foundation/Foundation.h>
#import "CocoaPitcher.h"
#import "CocoaBall.h"
#include "CppPitcher.h"
#include "CppBall.h"

int main(int argc, const char * argv[])
{

    @autoreleasepool {
        
        NSLog(@"*** ARC ***");
        CocoaPitcher *pitcher = [[CocoaPitcher alloc] init];

        @autoreleasepool {
            CocoaBall *ball = [[CocoaBall alloc] init];
            pitcher.ball = ball;
            [pitcher print];                
        }
        [pitcher print];
        
        NSLog(@"*** C++11 ***");
        std::shared_ptr<CppPitcher> cppPitcher(new CppPitcher);
        {
            std::shared_ptr<CppBall> ball(new CppBall);
            cppPitcher->ball = ball;
            cppPitcher->print();
        }
        cppPitcher->print();
    }
    return 0;
}

CocoaPitcher.h/m

#import <Foundation/Foundation.h>

@class CocoaBall;

@interface CocoaPitcher : NSObject

- (void)print;

@property (weak) CocoaBall *ball;

@end

#import "CocoaPitcher.h"
#import "CocoaBall.h"


@implementation CocoaPitcher
@synthesize ball;

- (void)print {
    NSLog(@"I'm %@, ball = %@", [self class], ball);
}
@end

CppPitcher.h/cpp

#include <memory>

class CppBall;

class CppPitcher {
public:
    void print();
    std::weak_ptr<CppBall> ball;
};

#include "CppPitcher.h"
#include "CppBall.h"

void CppPitcher::print(){
    printf("I'm CppPitcher, ");
    if (auto b = ball.lock()) {
        printf("ball = %x.\n", b.get());
    } else {
        printf("ball is null.\n");
    }
}
1 2