000
06.08.2009, 16:13 Uhr
deer
|
weiss jemand warum der Ausdruck bei (1) wahr ist? Wenn ich die Pointer (scp und st) ausgebe, haben sie verschiedene Addressen!? Hier ist der Output: scp: 0x9b37050, st: 0x9b37040, 0x9b37040 scp==st
übersehe ich was? Danke!
C++: |
#include <boost/shared_ptr.hpp> #include <unordered_map> #include <string> #include <stdio.h> using std::string; using boost::shared_ptr;
class Symbol { public: shared_ptr<Symbol> type; string name; Symbol() {} Symbol(string s, shared_ptr< Symbol > t) : name(s), type(t) {} virtual ~Symbol() { } };
typedef std::unordered_map<string, shared_ptr<Symbol>> symMap; typedef shared_ptr<Symbol> SymPtr;
class Scope { public: Scope *parent; Scope() : parent(0){} Scope(Scope* p) : parent(p) {} virtual ~Scope() { } };
class SymbolWithScope : public Symbol , public Scope{ public: symMap symbols; SymbolWithScope() : Symbol(), Scope() {} SymbolWithScope(Scope* p) : Scope(p) { printf("ctor SymbolWithScope\n"); } SymbolWithScope(string s, shared_ptr< Symbol > type) : Symbol(s, type) {} virtual ~SymbolWithScope() { symbols.clear(); } };
SymbolWithScope start_scope; Scope *scp = &start_scope;
void foo(SymPtr const& sym) { SymbolWithScope *st = dynamic_cast<SymbolWithScope*>(sym.get()); if(st) { printf("type is STRUCTSYMBOLSCOPE\n"); printf("scp: %p, st: %p, %p\n", scp, st, sym.get()); if(scp == st) printf("scp == st\n"); (1) } }
int main() { printf("----- begin ----- \n"); shared_ptr<SymbolWithScope> scp1(new SymbolWithScope(0)); scp = scp1.get(); foo(scp1); printf("----- end ----- \n"); }
|
|