OneZero
OneZero

Reputation: 11914

What happens when a function returns a class within a anonymous space?

class BaseClass {};

namespace { // anonymous
  class SubClass : BaseClass {};
}

BaseClass* somefunc() {
  return new SubClass();
}

Since SubClass is in the anonymous namespace, it's not accessible to anything outside the file, but somefunc is not in the anonymous space, therefore accessible. What happens if somefunc return the anonynmous class instance?

Upvotes: 2

Views: 67

Answers (1)

brettwhiteman
brettwhiteman

Reputation: 4452

Your function returns a BaseClass*. Anything in a different source file will not be able to cast it to a SubClass* since the class won't be accessible. In that case the caller is limited to using the pointer as a BaseClass pointer.

Upvotes: 1

Related Questions