-2

I am fairly new to C++ and I am trying to decode the piece of code shown below. In particular for the BaseSetAssoc::BlkType* line, I am not sure what the asterisk means in this case. I would appreciate some insight.

BaseSetAssoc::BlkType*
NMRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id)
{
    // Accesses are based on parent class, no need to do anything special
    BlkType *blk = BaseSetAssoc::accessBlock(addr, is_secure, lat, master_id);

    if (blk != NULL) {
        // move this block to head of the MRU list
        sets[blk->set].moveToHead(blk);
        DPRINTF(CacheRepl, "set %x: moving blk %x (%s) to MRU\n",
                blk->set, regenerateBlkAddr(blk->tag, blk->set),
                is_secure ? "s" : "ns");
    }

    return blk;
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

2 Answers2

1

BlkType isn't a member function, it's a type, possibly an enum or struct if not an inner class.

The BaseSetAssoc:: is needed to access such "inner" types (defined within a class, i.e. BaseSetAssoc).

So BaseSetAssoc::BlkType* is just a BaseSetAssoc::BlkType pointer.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Andrew Cheong
  • 29,362
  • 15
  • 90
  • 145
0

It's not "following", it's "preceding". As the comments have said: it means that it is returning a pointer to BaseSetAssoc::BlkType, rather than a whole BaseSetAssoc::BlkType.

What does this mean? It means mostly that the pointer can be NULL, or non-existent. Before using the result of this function, it is almost mandatory that you check if it is NULL first.

John Burger
  • 3,662
  • 1
  • 13
  • 23
  • And since we're talking c++ here, it should be checked against `nullptr` instead of `NULL`. – SingerOfTheFall Sep 22 '16 at 12:19
  • @SingerOfTheFall If the version of C++ is recent enough: sure! – John Burger Sep 22 '16 at 12:24
  • @JohnBurger C'mon its the current standard, under what rock you're living? – πάντα ῥεῖ Sep 22 '16 at 12:27
  • @πάνταῥεῖ: Er, there are many valid reasons to still be stuck on C++03. We're not all hobbyists on Github; some of us actually create and maintain enterprise infrastructure! – Lightness Races in Orbit Sep 22 '16 at 12:34
  • @πάνταῥεῖ Oh wow. Under the same rock that has students studying 8-bit microprocessors. The "current standard" is ever-changing: you need to adapt your answers to what the person is asking. If I'd've answered with: "`r15` can store 64-bit results!" how would that have answered them? – John Burger Sep 22 '16 at 12:35
  • _"It means mostly that the pointer can be NULL, or non-existent."_ I'd say that's tangential. The indirection is the important part. – Lightness Races in Orbit Sep 22 '16 at 12:35
  • _"Before using the result of this function, it is almost mandatory that you check if it is NULL first."_ This is also nonsense as far as general advice goes. – Lightness Races in Orbit Sep 22 '16 at 12:36
  • @LightnessRacesinOrbit Whenever I define a function, it either returns an Object (OK, a POD...), a reference, or a pointer. How do I decide? If it _could_ return a `NULL`, I return a pointer. That is the _**ONLY**_ reason that I return a pointer. Otherwise I'd be asking the coder to `delete` it for me... **\*SHUDDER\*** – John Burger Sep 22 '16 at 12:39
  • @JohnBurger: Ehm no. You're using pointers solely on the basis of whether you _should_ be returning a `boost::optional` or not, and not using pointers at all based on any of the reasons why you should return a pointer lol. Returning a pointer certainly doesn't mandate passing ownership to the caller; for example, `std::string::c_str()`.... which is never `NULL`, and must not be `delete`d by the coder... it's a view into an existing managed buffer stored elsewhere, which is a much more useful reason to return a pointer! – Lightness Races in Orbit Sep 22 '16 at 14:23
  • @LightnessRacesinOrbit Look at the code. `BlkType *blk = ...; if (blk!=NULL) {... } return blk;` In other words, it could _easily_ return `NULL`. Thus my reason stands. And you missed my not-so-subtle \* **SHUDDER** \* - the mere _concept_ of returning a pointer to suggest that the coder should `delete` it, is anathema to me (that is, I don't like it!) – John Burger Sep 22 '16 at 14:32